- 单继承中的构造顺序问题
-
先构造父类的再构造子类的,析构顺序相反
-
- 多继承中构造顺序问题
-
任何构造顺序问题都和初始化参数列表无关
-
构造顺序和继承顺序一致
-
class A { public: A() { cout << "A"; } ~A() { cout << "A" ; } protected: }; class B :public A { public: B() { cout << "B"; } ~B() { cout << "B" ; } }; int main() { B b; return 0; }多继承
#includeusing namespace std; class A { public: A() { cout << "A"; } ~A() { cout << "A" ; } protected: }; class B :public A { public: B() { cout << "B"; } ~B() { cout << "B" ; } }; class C { public: C() { cout << "C"; } ~C() { cout << "C"; } }; class D { public: D() { cout << "D"; } ~D() {cout << "D";} }; //构造顺序和继承顺序一致:先继承C,再继承A,最后继承D class F :public C, public A, public D { public: //任何构造顺序问题,都和初始化参数列表无关 F() { cout << "F";} ~F() { cout << "F";} }; //CADF FDAC int main() { F f; return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)