派生类的构造函数要点(from C++ primer plus):
1、首先创建基类对象;
2、派生类的构造函数应通过成员初始化列表将基类信息传递给基类的构造函数;
3、派生类构造函数应初始化派生类新增的数据成员。
归纳:创建基类对象(按照继承的顺序)→初始化类中新成员(按成员定义的顺序)→派生类的构造函数
#include <iostream> #include <cstring> #include <vector> #include <list> #include <string> #include <array> using namespace std; class B { public: B(void) { cout << "Bt"; } ~B(void) { cout << "~Bt"; } }; struct C { C(void) { cout << "Ct"; } ~C(void) { cout << "~Ct"; } }; struct D : B { D(){ cout << "Dt"; } ~D(){ cout << "~Dt"; } private: C c; }; int main() { D d; return 0; }
虚基类的构造函数在任何非虚基类构造函数前调用。如果继承于多个虚基类,他们的调用顺序由继承顺序为准。
#include <iostream> using namespace std; class OBJ1 { public: OBJ1(){ cout << "OBJ1n"; } ~OBJ1(){ cout << "~OBJ1n"; } }; class OBJ2 { public: OBJ2(){ cout << "OBJ2n"; } ~OBJ2(){ cout << "~OBJ2n"; } }; class Base1 { public: Base1(){ cout << "Base1n"; } ~Base1(){ cout << "~Base1n"; } }; class Base2 { public: Base2(){ cout << "Base2n"; } ~Base2(){ cout << "~Base2n"; } }; class Base3 { public: Base3(){ cout << "Base3n"; } ~Base3(){ cout << "~Base3n"; } }; class Base4 { public: Base4(){ cout << "Base4n"; } ~Base4(){ cout << "~Base4n"; } }; class Derived :public Base1, virtual public Base4, public Base3, virtual public Base2 { public: Derived() :Base4(), Base3(), Base2(), Base1(), obj2(), obj1() { cout << "Derived ok.n"; } protected: OBJ1 obj1; OBJ2 obj2; }; int main() { Derived aa; cout << "This is ok.n"; return 0; }
派生类的析构顺序:
#include <iostream> using namespace std; class Base { public: Base() { cout << "Base contruction" << endl; } ~Base() { cout << "Base deconstruction" << endl; } }; class Derived : public Base { int num; public: Derived(int i) { num = i; cout << "Derived contruction " << num << endl; } ~Derived() { cout << "Derived deconstruction" << num << endl; } }; int main() { Derived derived(1); cout << "------------" << endl; Base* basePtr; cout << "------------" << endl; Derived* derevedPtr; cout << "------------" << endl; basePtr = new Derived(2); cout << "------------" << endl; delete basePtr; cout << "------------" << endl; return 0; }
可以看出,当 new CDerive() 时,会先运行基类的构造函数,然后再运行派生类的构造函数;
而当 delete pointer 时,编译器只考虑 pointer 指针本身的类型而不关心 pointer 实际指向的类型,
即:若 pointer 为基类指针,则只调用基类的析构函数(不管 pointer 实际指向的是基类还是派生类);
若 pointer 是派生类指针,则先调用派生类的析构函数,再调用基类的析构函数,调用顺序与调用构造函数的顺序相反。
下面在析构函数前加关键字virtual后发现有所变化。
#include <iostream> using namespace std; class Base { public: Base() { cout << "Base contruction" << endl; } virtual ~Base() { cout << "Base deconstruction" << endl; } }; class Derived : public Base { int num; public: Derived(int i) { num = i; cout << "Derived contruction " << num << endl; } virtual ~Derived() { cout << "Derived deconstruction" << num << endl; } }; int main() { Derived derived(1); cout << "------------" << endl; Base* basePtr; cout << "------------" << endl; Derived* derevedPtr; cout << "------------" << endl; basePtr = new Derived(2); cout << "------------" << endl; delete basePtr; cout << "------------" << endl; return 0; }
原文链接: https://www.cnblogs.com/maider/p/11248309.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/299755
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!