构造和析构 的顺序

#include <iostream>
#include <stdlib.h>

using namespace std;

class B
{
public:
B()
{
cout << "B()" << endl;
}

~B()
{
cout << "~B()" << endl;
}
int m_b1;
int m_b2;


};

class A
{
public:
A()
{
m_pb1 = new B;
m_pb2 = new B;
cout << "A()" << endl;
}

~A()
{
delete m_pb1;
m_pb1 = nullptr;

delete m_pb2;
m_pb2 = nullptr;
cout << "~A()" << endl;
}
B *m_pb1;
B *m_pb2;


};


int main(){

A *pa1 = new A;
delete pa1;
pa1 = nullptr;

system("pause");
return 0;
}

posted @ 2017-09-11 22:03  w6811645  阅读(153)  评论(0编辑  收藏  举报