标签:
#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout << "A create" << endl;
}
virtual ~A()
{
cout << "A delete" << endl;
}
};
class B:public A
{
public:
B()
{
cout << "B create" << endl;
}
~B()
{
cout << "B delete" << endl;
}
};
int main()
{
A *p = new B;
//因为这里的p是基类的指针.但指向子类的对象.所以.
//对p操作首先就是操作子类的相关成员,因此delete p首先就是调用子类的析构函数.一旦调用子类的析构函数.也是首先调用父类的析构函数.由此避免内存泄漏
delete p;
cin.get();
return 0;
}
标签:
原文地址:http://www.cnblogs.com/c-slmax/p/4746501.html