我想从 A 类继承,但是 A 的析构函数不是虚拟的,我不能修改 A 的定义。如何避免以下情况?
struct A
{
A()
: a(new char[8])
{}
~A()
{
delete[] a;
}
char* a;
}
struct B : A
{
B()
: A(), b(new char[8])
{}
~B()
{
delete[] b;
}
char* b;
};
int main()
{
A* p_a = new B;
delete p_a; // How to avoid such a dangerous deletion?
}