構造一個類的對象的一般順序是:
(1):如果有直接或間接的虛基類,先執行虛基類的構造函數
(2):如果尤其它基類,則按照他們在繼承聲明列表中的出現次序,分別執行他們的構造函數,但構造過程中,不再執行虛基類的構造函數。
[csharp]
#include<iostream>
using namespace std;
class base0{
public: www.2cto.com
base0(int var):var0(var){cout<<"ggggg"<<endl;};
int var0;
void fun0(){cout<<"memory of base0"<<endl;}
~base0(){cout<<"gggg"<<endl;}
};
class base1:virtual public base0{
public:
base1(int var):base0(var){cout<<"hhhhh"<<endl;}
int var1;
~base1(){cout<<"HHHH"<<endl;}
};
class base2:virtual public base0{
public:
base2(int var):base0(var){cout<<"tttttt"<<endl;}
int var2;
};
class mm:public base1,public base2{
public:
mm(int var):base1(var),base0(var),base2(var){cout<<"ppppp"<<endl;}
int var;
void fun(){cout<<"member of mm"<<endl;}
~mm(){cout<<"ppppp"<<endl;}
};
int main(){
mm a(1);
a.var=2;
a.fun();
return 0;
}