Simple C ++ object model-understanding the execution sequence of constructor and destructor

Source: Internet
Author: User

This article mainly describes the execution sequence of the constructor during object creation, the initialization sequence of object members, the execution sequence of the Destructor during object destruction, and the destruction sequence of object members.

"Object Construction starts from the root of the class hierarchy. In each layer, the base class constructor is called first, and then the member object constructor is called. The structure is executed strictly in the order opposite to the structure. This order is unique. Otherwise, the compiler cannot automatically execute the structure process.

An interesting phenomenon is that the initialization order of member objects is not affected by their order in the initialization table. It is determined only by the Order declared by the member objects in the class. This is because the class declaration is unique, and the class constructor can have multiple, so there will be multiple initialization tables in different order. If the member object is constructed in the order of the initialization table, the Destructor cannot obtain a unique reverse order ." (Referenced from references [1])

 

From this point of view, there are reasons for the existence of each language feature. Learning these features is the reason for understanding the existence of these features.

The following code describes the preceding sections. Four classes are Foo, bar, base, derived, their constructor, copy constructor, and destructor all have information output.

#include <iostream>using namespace std;class Foo{public:        Foo() { cout << "Foo constructor" << endl; }        Foo(const Foo &foo) { cout << "Foo copy constructor" << endl; }        ~Foo() { cout << "Foo deconstructor" << endl; }};class Bar{public:        Bar() { cout << "Bar constructor" << endl; }        Bar(const Bar &bar) { cout << "Bar copy constructor" << endl; }        ~Bar() { cout << "Bar deconstructor" << endl; }};class Base{public:        Base() { cout << "Base constructor" << endl; }        ~Base() { cout << "Base deconstructor" << endl; }};class Derived : public Base{public:        Derived() { cout << "Derived constructor without arguments" << endl; }        Derived(const Foo &foo, const Bar &bar);        Derived(const Bar &bar, const Foo &foo);        ~Derived() { cout << "Derived deconstructor" << endl; }private:        Foo m_foo;        Bar m_bar;};Derived::Derived(const Foo &foo, const Bar &bar) :        m_foo(foo),        m_bar(bar){        cout << "Derived constructor with argument[Foo foo, Bar bar] passed by references" << endl;}Derived::Derived(const Bar &bar, const Foo &foo) :        m_bar(bar),        m_foo(foo){        cout << "Derived constructor with argument[Bar bar, Foo foo] passed by references" << endl;}int main (int argc, char** argv){        Foo foo;        Bar bar;        cout << "test case 1:" << endl;        Derived deri_1;  //  (1)        cout << "test case 2:" << endl;        Derived deri_2(foo, bar);   //  (2)        cout << "test case 3:" << endl;        Derived deri_3(bar, foo);   //  (3)        cout << "test case end" << endl;        return 0;}

The execution result is:

The printed information can be divided into several parts:

(1) create objects Foo and bar and execute the constructors of Foo and bar.

(2) Test Case 1: Create the object deri_1. First, execute the base-class constructor, and then execute the constructor m_foo and m_bar to construct the member, finally, call your own Constructor (No parameter ).

(3) Test Case 2: Create the object deri_2. The call sequence is the same as that of Case 1.

(4) Test Case 3: Create the object deri_3. The call sequence is the same as that of Case 1. Note that the creation and execution of deri_2 and deri_3 are different derived constructors. Although the constructor parameters are in different order, the order of the constructor members is the same.

(5) destroy objects deri_3, deri_2, deri_1. The execution sequence of destructor is the same, which is opposite to that of constructor. The C ++ Standard specifies that these objects are destroyed in the reverse order of object declarations.

(6) destroy the bar and foo objects.

Compile and run the environment:

$ uname -aLinux localhost.localdomain 2.6.18-308.el5 #1 SMP Fri Jan 27 17:17:51 EST 2012 x86_64 x86_64 x86_64 GNU/Linux$ g++ --versiong++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-52)

References:

[1] High Quality C ++ programming guide: http://oss.org.cn/man/develop/c&c++/c/c.htm

[2] http://stackoverflow.com/q/15948381/1145750

Repost this article please indicate the author and the source [Gary's influence] http://garyelephant.me, do not for any commercial purposes!

Author: Gary Gao (garygaowork [at] gmail.com) focuses on the internet, distributed, high-performance, nosql, automation, and software teams

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.