草庐IT

c++ - static_pointer_cast<Derived> pReallyABase = static_pointer_cast<Derived>(pBase) 有效!为什么?

这个问题在这里已经有了答案:Downcastingusingthe'static_cast'inC++(3个答案)关闭8年前。我不明白为什么会这样。pReallyABase是一个向下转换的shared_pointer,它指向一个基类实例。我理解为什么编译器让我调用pReallyABase->onlyForDerived()因为我将它定义为派生类指针,但是当我尝试使用该指针调用派生类函数时为什么没有出现运行时错误?classBase{public:virtualstringwhatAmI(){return"IamaBase";}};classDerived:publicBase{publ

c++ - 我可以创建一个 auto_ptr 数组吗?

我有一个基类,它被多个派生类继承。我想创建baseClass指针的自动指针数组。当我初始化那些自动指针时,我遇到了一些编译时错误,然后我尝试这样做std::auto_ptrpbase[3];std::auto_ptrb1(newderived1());std::auto_ptrb2(newderived2());std::suto_ptrb3(newderived3());pbase[0]=b1;pbase[1]=b2;pbase[2]=b3;它工作正常,我修复了内存泄漏问题,而我是一个窗口,我不使用valgrind,我使用boost框架来解决泄漏问题。对于编译错误:classA{pu