关于C++构造析构顺序的总结

起因

之前对C++基类派生类构造函数和析构函数的调用顺序存在疑惑,因此写了个小程序验证一下。

代码

#include <iostream>
using namespace std;

class Base
{
public:
	Base()
	{
		cout << "Base::Base()" << endl;
	}

	~Base()				//case3
//	virtual ~Base()		//case4
	{
		cout << "Base::~Base()" << endl;
	}
};

class Derived : public Base
{
public:
	Derived()
	{
		cout << "Derived::Derived()" << endl;
	}
	~Derived()
	{
		cout << "Derived::~Derived()" << endl;
	}
};

int main()
{
	cout << "------case1: Base Object------------------------------" << endl;
	Base *p = new Base();
	delete p;

	cout << "\n\n";
	cout << "------case2: Derived Object---------------------------" << endl;
	Derived *p3 = new Derived();
	delete p3;

	cout << "\n\n";
	cout << "------case3/case4: Base pointer to Derived Object-----" << endl;
	cout << "------(not virtual function/virtual function)---------" << endl;
	Base *p2 = new Derived();
	delete p2;

	return 0;
}

输出

------case1: Base Object------------------------------
Base::Base()
Base::~Base()


------case2: Derived Object---------------------------
Base::Base()
Derived::Derived()
Derived::~Derived()
Base::~Base()


------case3: Base pointer to Derived Object-----
------(not virtual function)---------
Base::Base()
Derived::Derived()
Base::~Base()


------case4: Base pointer to Derived Object-----
------(virtual function)---------
Base::Base()
Derived::Derived()
Derived::~Derived()
Base::~Base()

分析

  1. 由case1可知,
    基类对象在创建的时候会自动调用构造函数;
    销毁时会自动调用析构函数。

  2. 由case2可知,
    普通派生类对象在创建时会先自动调用基类构造函数,然后调用自身构造函数;
    销毁时会先自动调用自身的析构函数,然后调用基类的析构函数。

  3. 由case3/case4可知,
    基类指针指向的派生类对象在创建时先自动调用基类构造函数,然后调用自身构造函数;
    销毁时先判断,
          如果析构函数不是虚函数,则当做基类对象析构————自动调用基类析构函数,
          如果析构函数是虚函数,则当做派生类对象————先自动调用派生类析构函数,然后调用基类构造函数。
      

结论

综上,派生类对象构造析构的过程是这样的:

创建对象时,调用的构造函数只跟创建的对象类型有关,
判断对象类型,
      如果是基类对象,自动调用自身构造函数
      如果是派生类对象,先自动调用基类构造函数,然后调用自身构造函数。
  
销毁对象时,调用的析构函数跟 析构函数是否是虚函数、指向它的指针类型 和 析构的对象类型有关
判断构造函数是否是为虚函数
      如果构造函数不是虚函数,按照指针类型(静态类型)析构
            如果指针类型是基类类型
            如果指针类型是派生类类型
      如果构造函数是虚函数,按照对象类型(动态类型)析构
            如果析构对象是基类类型
            如果析构对象是派生类类型
  

本文章迁移自http://blog.csdn.net/timberwolf_2012/article/details/40682855

/** * RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS. * LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/ /* var disqus_config = function () { this.page.url = PAGE_URL; // Replace PAGE_URL with your page's canonical URL variable this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable }; */ (function() { // DON'T EDIT BELOW THIS LINE var d = document, s = d.createElement('script'); s.src = 'https://chenzz.disqus.com/embed.js'; s.setAttribute('data-timestamp', +new Date()); (d.head || d.body).appendChild(s); })();