虛析構函數,派生類調用基類構造方法
阿新 • • 發佈:2017-10-20
div pub urn new turn col blog 徹底 ()
#include <iostream> using namespace std; class A{ public: A() { cout<<"construct A"<<endl; } A(int a) { cout<<"construct int A"<<endl; } virtual ~A() //析構函數 必須 聲明為 虛函數,才能徹底釋放內存空間 { cout<<"destory A"<<endl; } }; class B : public A{ public: B() { cout<<"construct B"<<endl; } B(int a):A(a) //構造器調用A中int構造方法{ cout<<"construct int B"<<endl; } ~B() { cout<<"destory B"<<endl; } }; class C:public B{ public: C() { cout<<"construct C"<<endl; } ~C() { cout<<"destory C"<<endl; } }; int main() { A *p = new C(); delete p; return 0; }
虛析構函數,派生類調用基類構造方法