我有3节课
class A
{
A();
virtual ~A();
}
class B : public A
{
B();
~B();
}
class C
{
void *obj;
C() : obj(nullptr) {}
~C() { if (obj) delete obj; }
}
当我将类C
用作类的任何子级的容器A
并尝试删除C
实例时。A
,B
没有调用析构函数是正常的吗?解决方案是什么?
C* instance = new C();
instance.obj = new B();
//Magic
delete instance; // A and B destructor is not called