Tag: downcast

如何降低一个std :: shared_ptr?

考虑: struct SomethingThatsABase { virtual bool IsChildOne() const { return false; } virtual bool IsChildTwo() const { return false; } }; struct ChildOne : public SomethingThatsABase { virtual bool IsChildOne() const { return true; } }; struct ChildTwo : public SomethingThatsABase { virtual bool IsChildTwo() const { return true; } }; void SomeClientExpectingAChildOne(std::shared_ptr<ChildOne> const& ptrOne) […]

在C ++中何时非法上传?

我非常肯定我理解了向上转换和向下转换之间的一般区别,尤其是在C ++中。 我明白,我们不能总是downcast,因为将基类指针投射到派生类指针会假定指向的基类对象具有派生类的所有成员。 在这个学期的初期,我的教授告诉全class学生,在C ++中有时候也是非法的,但是我似乎错过了在笔记里为什么不记得什么时候发生的事情。 任何人都可以告诉我什么时候在C ++中使用upcast是非法的?