查看 C++ 语言标准,有没有办法只调用派生类析构函数,而不调用基类的析构函数?
所以,对于类
class Base { public: virtual ~Base() {} };
class Derived : public Base { public: ~Derived();};
如果可以编写类似的代码
Base *basePtr = new Derived();
//do something with basePtr
// Now somehow destroy Derived while keeping Base - call ~Derived() only,
// line below however will call both ~Derived() and ~Base() - how it can be done?
dynamic_cast<Derived*>(basePtr)->~Derived();
因此,执行上述代码后,basePtr 将仅指向 Base 对象,就像它是由
Base *basePtr = new Base();
加上在调用 new Derived() 和销毁 Derived 类之间操作 basePtr 引起的对 Base 对象的任何修改?
或者,这是禁止的,不可能做到的?