1、构造函数的调用顺序

基类构造函数、对象成员构造函数、派生类本身的构造函数

2、析构函数的调用顺序

派生类本身的析构函数、对象成员析构函数、基类析构函数(与构造顺序正好相反)

3、特例

局部对象,在退出程序块时析构

静态对象,在定义所在文件结束时析构

全局对象,在程序结束时析构

继承对象,先析构派生类,再析构父类

对象成员,先析构类对象,再析构对象成员

4、例子

#include <iostream>
using namespace std; class Base1
{
public:
Base1(void){cnt++;cout<<"Base1::constructor("<<cnt<<")"<<endl;}
~Base1(void){cnt--;cout<<"Base1::deconstructor("<<cnt + <<")"<<endl;}
private:
static int cnt;
};
int Base1::cnt = ; class Base2
{
public:
Base2(int m){num = m; cout<<"Base2::constructor("<<num<<")"<<endl;}
~Base2(void){cout<<"Base2::deconstructor("<<num<<")"<<endl;}
private:
int num;
}; class Example
{
public:
Example(int n){num = n; cout<<"Example::constructor("<<num<<")"<<endl;}
~Example(void){cout<<"Example::deconstructor("<<num<<")"<<endl;}
private:
int num;
}; class Derived:public Base1, public Base2
{
public:
Derived(int m, int n):Base2(m),ex(n){cnt++;cout<<"Derived::constructor("<<cnt<<")"<<endl;}
~Derived(void){cnt--;cout<<"Derived::deconstructor("<<cnt+<<")"<<endl;}
private:
Example ex;
static Example stex; //Example::constructor(1) //不能输出
static int cnt;
};
int Derived::cnt = ; Derived ge_a(,); // Base1::constructor(1)
// Base2::constructor(1)
// Example::constructor(2)
// Derived::constructor(1)
static Derived gs_b(,); // Base1::constructor(2)
// Base2::constructor(3)
// Example::constructor(4)
// Derived::constructor(2)
int main(void)
{
cout << "---------start---------" << endl;
Derived d(,); // Base1::constructor(3)
// Base2::constructor(5)
// Example::constructor(6)
// Derived::constructor(3)
Derived e(,); // Base1::constructor(4)
// Base2::constructor(7)
// Example::constructor(8)
// Derived::constructor(4)
cout << "----------end----------" << endl; //Derived e(7,8) 析构
// Derived::deconstructor(4)
// Example::deconstructor(8)
// Base2::deconstructor(7)
// Base1::deconstructor(4) //Derived d(5,6) 析构
// Derived::deconstructor(3)
// Example::deconstructor(6)
// Base2::deconstructor(5)
// Base1::deconstructor(3)
return ;
}
//static Derived gs_b(3,4) 析构
// Derived::deconstructor(2)
// Example::deconstructor(4)
// Base2::deconstructor(3)
// Base1::deconstructor(2)
//Derived ge_a(1,2) 析构
// Derived::deconstructor(1)
// Example::deconstructor(2)
// Base2::deconstructor(1)
// Base1::deconstructor(1) //static Example stex 析构
//Example::deconstructor(1) //不能输出 
#include <iostream>
using namespace std;
class A
{
public:
A(){cout<<"A::constructor"<<endl;};
~A(){cout<<"A::deconstructor"<<endl;};
};
class B
{
public:
B(){cout<<"B::constructor"<<endl;};
~B(){cout<<"B::deconstructor"<<endl;};
};
class C : public A
{
public:
C(){cout<<"C::constructor"<<endl;};
~C(){cout<<"C::deconstructor"<<endl;};
private:
// static B b;
B b;
};
class D : public C
{
public:
D(){cout<<"D::constructor"<<endl;};
~D(){cout<<"D::deconstructor"<<endl;};
}; int main(void)
{
C* pd = new D();
delete pd;
return ;
}
/* Output
----->B b
A::constructor
B::constructor
C::constructor
D::constructor
C::deconstructor
B::deconstructor
A::deconstructor ----->static B b
A::constructor
C::constructor
D::constructor
C::deconstructor
A::deconstructor
*/

C++构造函数和析构函数的调用顺序的更多相关文章

  1. C++C++中构造函数与析构函数的调用顺序

    http://blog.csdn.net/xw13106209/article/details/6899370 1.参考文献 参考1: C++继承中构造函数.析构函数调用顺序及虚函数的动态绑定 参考2 ...

  2. C++ 构造函数和析构函数的调用顺序、虚析构函数的作用

    构造函数和析构函数的调用顺序 构造函数的调用顺序: 当建立一个对象时,首先调用基类的构造函数,然后调用下一个派生类的构造函数,依次类推,直至到达最底层的目标派生类的构造函数为止. 析构函数的调用书序: ...

  3. C++中构造函数和析构函数的调用顺序

    一般而言,析构函数调用的顺序和构造函数调用顺序相反,但是,对象的存储类别可以改变调用析构函数的顺序.举例说明: CreateAndDestroy类的定义 CreateAndDestroy类的成员函数的 ...

  4. C++学习笔记(7)----类的数组中构造函数和析构函数的调用顺序

    C++类的数组中构造函数和析构函数的调用顺序(2) 对于如下的代码: #include<iostream> using namespace std; class CBase { priva ...

  5. C++继承,多重继承,虚继承的构造函数以及析构函数的调用顺序问题

    #include <iostream> using namespace std; class A{ int data_a; public: A(){ data_a = ; cout < ...

  6. C++:派生类的构造函数和析构函数的调用顺序

    一.派生类 在C++编程中,我们在编写一个基类的派生类时,大致可以分为四步: • 吸收基类的成员:不论是数据成员还是函数成员,派生类吸收除基类的构造函数和析构函数之外的全部成员. • 改造基类函数:在 ...

  7. c++学习笔记4,派生类的构造函数与析构函数的调用顺序(一)

    測试源代码: //測试派生类的构造函数的调用顺序何时调用 //Fedora20 gcc version=4.8.2 #include <iostream> using namespace ...

  8. 【校招面试 之 C/C++】第10题 C++不在构造函数和析构函数中调用虚函数

    1.不要在构造函数中调用虚函数的原因 在概念上,构造函数的工作是为对象进行初始化.在构造函数完成之前,被构造的对象被认为“未完全生成”.当创建某个派生类的对象时,如果在它的基类的构造函数中调用虚函数, ...

  9. C++学习笔记(6)----基类和派生类的构造函数和析构函数的执行顺序

    基类和派生类:构造函数和析构函数的执行顺序 在Visual Studio中,新建控制台工程,构造类如下: #include<iostream> using namespace std; c ...

随机推荐

  1. opencv+codeblocks +anaconda

    study from : https://www.jianshu.com/p/c16b7c870356 #include <cstdio> #include <cstdlib> ...

  2. Markdown——入门指南

    导语: Markdown 是一种轻量级的「标记语言」,它的优点很多,目前也被越来越多的写作爱好者,撰稿者广泛使用.看到这里请不要被「标记」.「语言」所迷惑,Markdown 的语法十分简单.常用的标记 ...

  3. python基础1之 由来、种类、优缺点、安装环境

    python基础1之由来.种类.优缺点.安装环境 一.前世今生 Python的创始人是吉多·范罗苏姆(Guido van Rossum),在1989年开发.今年最新的编程语言排行榜中,python名列 ...

  4. ARIMA模型原理

    一.时间序列分析 北京每年每个月旅客的人数,上海飞往北京每年的游客人数等类似这种顾客数.访问量.股价等都是时间序列数据.这些数据会随着时间变化而变化.时间序列数据的特点是数据会随时间的变化而变化. 随 ...

  5. DES加密ECB(模式) golang

    Java默认DES算法使用DES/ECB/PKCS5Padding,而golang认为这种方式是不安全的,所以故意没有提供这种加密方式,那如果我们还是要用到怎么办?下面贴上golang版的DES EC ...

  6. 上传代码到github

    上传代码前需配置连接秘钥和设置本地git账号密码. 1.检查上传文件目录状态 git status 2.将更改文件添加到缓存区 git add . 3.添加本次代码更改说明 git commit -m ...

  7. external与static的用法

    一.extern是C/C++语言中表明函数和全局变量作用范围(可见性)的关键字:它告诉编译器,其声明的函数和变量可以在本函数文件或其它函数文件中使用. 1.对于extern变量来说,仅仅是一个变量的声 ...

  8. Python自动化中的元素定位(一)

    1.使用selenium中的webdriver模块对浏览器进行操作 1)from selenium import webdriver 加载模块 2)b = webdriver.Friefox() 打开 ...

  9. PostgreSQL学习笔记(一)-安装PostgreSQL

    PostgreSQL官网:https://www.postgresql.org/docs/11/index.html1.如何安装2.如何修改配置文件3.如何设置自动启动4.如何修改数据用户密码 本文环 ...

  10. SVM小白教程(2):拉格朗日对偶

    在上一篇文章中,我们推导出了 SVM 的目标函数: \[ \underset{(\mathbf{w},b)}{\operatorname{min}} ||\mathbf{w}|| \\ \operat ...