delete 与析构函数 再次发问??
class B{public: B *b; B() {} ~B() { cout<<"~B() 被调用"<<endl; system("pause"); } B* functionB() { b = new B(); cout<<"functionB 被调用"<<endl; return b; }};class A{public: B *a_b; A() {} ~A(void) { cout<<"~A() 被调用"<<endl; delete a_b; } void functionA() { a_b = new B(); cout<<"functionA 被调用"<<endl; a_b->functionB(); }};void main(){ A *a = new A(); a->functionA(); delete a;}输出内容:functionA 被调用 functionB 被调用 ~A() 被调用 ~B() 被调用
B* functionB() { b = new B(); cout<<"functionB 被调用"<<endl; return b; }在functionB中我们创建了B,但是没有删除。