派生类构造函数的理解
我大概试了派生类构造函数的所有情况吧,怕忘掉所以在这里整理一下
………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
第一种就是书上例题上的那种最简单的情况
#include<iostream>
using namespace std;
class Base1{
public:
Base1(int i){cout<<"Constructing Base1"<<i<<endl;
}
~Base1(){cout<<"Destroying Base1"<<endl;}
};
class Base2{
public:
Base2(int j){cout<<"Constructing Base2"<<j<<endl;
}
~Base2(){cout<<"Destroying Base2"<<endl;
}
};
class Base3{
public:
Base3(){cout<<"Constructing Base3*"<<endl;
}
~Base3(){cout<<"Destroying Base3*"<<endl;
}
};
class Derived:public Base2,public Base1,public Base3{
public:
Derived(int a,int b,int c,int d):Base1(a),member2(d),member1(c),Base2(b){
}
~Derived(){cout<<"Destroying Derived"<<endl;
}
private:
Base1 member1;
Base2 member2;
Base3 member3;
};
int main()
{
Derived obj(1,2,3,4);
return 0;
}
………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
第二种,我把继承方式换成了私有继承(这里容易形成的误区就是类的对象不是不能调用私有成员吗,那只是对继承下来的那些东西的限制吧,毕竟构造函数是不能继承的)
#include<iostream>
using namespace std;
class Base1{
public:
Base1(int i){cout<<"Constructing Base1"<<i<<endl;
}
~Base1(){cout<<"Destroying Base1"<<endl;}
};
class Base2{
public:
Base2(int j){cout<<"Constructing Base2"<<j<<endl;
}
~Base2(){cout<<"Destroying Base2"<<endl;
}
};
class Base3{
public:
Base3(){cout<<"Constructing Base3*"<<endl;
}
~Base3(){cout<<"Destroying Base3*"<<endl;
}
};
class Derived:private Base2,private Base1,private Base3{
public:
Derived(int a,int b,int c,int d):Base1(a),member2(d),member1(c),Base2(b){
}
~Derived(){cout<<"Destroying Derived"<<endl;
}
private:
Base1 member1;
Base2 member2;
Base3 member3;
};
int main()
{
Derived obj(1,2,3,4);
return 0;
}
…………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
第三种,就是最全面的一种了,我把Derived的构造函数体里加了内容
#include<iostream>
using namespace std;
class Base1{
public:
Base1(int i){cout<<"Constructing Base1"<<i<<endl;
}
~Base1(){cout<<"Destroying Base1"<<endl;}
};
class Base2{
public:
Base2(int j){cout<<"Constructing Base2"<<j<<endl;
}
~Base2(){cout<<"Destroying Base2"<<endl;
}
};
class Base3{
public:
Base3(){cout<<"Constructing Base3*"<<endl;
}
~Base3(){cout<<"Destroying Base3*"<<endl;
}
};
class Derived:public Base2,public Base1,public Base3{
public:
Derived(int a,int b,int c,int d):Base1(a),member2(d),member1(c),Base2(b)
{
cout<<"Constructing Derived"<<endl;
}
~Derived(){cout<<"Destroying Derived"<<endl;
}
private:
Base1 member1;
Base2 member2;
Base3 member3;
};
int main()
{
Derived obj(1,2,3,4);
return 0;
}
…………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
下面这个也没什么了,就是比较复杂而已(因为嵌套了一些)
//这里C继承B和A,内嵌D,E继承C;调用构造函数情况:
#include<iostream>
using namespace std ;
class D
{
public:
D(){cout<<"initializing D..."<<endl;}
~D(){cout<<"destroying D..."<<endl;}
};
class A
{
public:
A(){cout<<"initializing A..."<<endl;}
~A(){cout<<"destroying A..."<<endl;}
};
class B
{
public:
B(){cout<<"initializing B..."<<endl;}
~B(){cout<<"destroying B..."<<endl;}
};
class C : private B , private A
{
public:
D d;
C(){cout<<"initializing C..."<<endl;}
~C(){cout<<"destroying C..."<<endl;}
};
class E: private C
{
public:
E(){cout<<"initializing E..."<<endl;}
~E(){cout<<"destroying E..."<<endl;}
};
int main()
{
E e;
return 0 ;
}
………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
总结:
多重继承和有内嵌对象时构造函数调用顺序
1、调用按照声明顺序的基类构造函数 2、调用内嵌对象构造函数 3、调用自身构造函数
………………………………………………………………………………………………………………………………………………………………………………………………………………………………
还有就是一些注意点吧
首先必须清楚一点:初始化与赋值是两码事!
派生类的构造函数的初始化列表可以包含基类的构造函数、派生类成员的初始化,但是不能有基类成员的初始化!