#include <iostream>
using namesapce std;
class A
{
public:
virtual ~A(){cout<<"delete A"<<endl};
};
class B: public A
{
public:
B(int n):n(n){}
void show(){cout<<n<<endl;}
~B(){cout<<"delete B"<<endl;}
private:
int n;
}
int main()
{
A *pa;
B *pb = new B(1);
pa = pb;
delete pa;
pb->show();
return 0;
}
when destructor of calss A is virtual ~A(){...}
,the output of program:
delete B
delete A
1
when destructor of class A is ~A(){...}
, the output of progarm:
delete A
0
why the value of n is different, when destructor of class A is virtual or non virtual? when call destructor of B to destroy object, why the calss member n is still existent?