这里我遇到了对象的析构函数中的问题。在下面给出的程序中,我试图动态地使用基类指针创建对象,当我尝试删除时指针比指针只删除 A 的对象。 那么如何在程序结束时删除所有对象?
class A{
public:
A(){
cout<<"Object A is created "<<endl;
}
~A(){
cout<<"Object A is destroyed"<<endl;
}
};
class B{
public:
B(){
cout<<"object B is Created \n";
}
~B(){
cout<<"Object B is Destroyed"<<endl;
}
};
class C{
public:
C(){
cout<<"constructor of C class"<<endl;
}
~C(){
cout<<"destructor of C "<<endl;
}
};
class D:public C, public B, public A{
public:
D(){
cout<<"Object D is created "<<endl;
}
~D(){
cout<<"Object D is destroyed "<<endl;
}
};
int main()
{
A *a;
a = new D();
delete a;
return 0;
}