Initialization order of C + + class objects ZZ

Source: Internet
Author: User

C + + Constructor Call Order

1. Create the object of the derived class, the constructor of the base class is called first (also precedence over the member class in the derived class);

2. If there is a member class inside the class, the constructor of the member class is called first; (also precedence over the constructor of the class itself)

3. base class constructors If there are multiple base classes, the order in which the constructors are called is the order in which a class appears in the class-derived table rather than the order in which they are in the Member initialization table;

4. Member class object constructors If there are multiple member class objects, the order in which the constructors are called is the order in which the objects are declared in the class, not the order in which they appear in the Member initialization table;

5. Derived class constructors, as a general rule derived class constructors should not be able to assign values directly to a base class data member but to pass values to the appropriate base class constructor, otherwise the implementation of the two classes becomes tightly coupled (tightly coupled) will make it more difficult to correctly modify or extend the implementation of the base class. (The responsibility of the base class Designer is to provide a set of appropriate base class constructors)

It can be concluded that the sequence of initialization:

Parent class constructor –> Member class object constructor –> self constructor

Where the initialization of member variables is related to the order of Declaration, the order in which the constructors are called is the order of the class-derived list.
The destruction sequence is opposite to the construction order

The following is the topic of an online company with a recent written test:

#include <iostream>
using namespace Std;

Class A
{
Public
A () {cout<< "a" <<ENDL;}
Virtual ~a () {cout<< "~a" <<endl;}
};

Class B:public A
{
Public
B () {cout<< "B" <<ENDL;}
~b () {cout<< "~b" <<endl;}
Private
A;
};

Class C:public A, Public B//class inherit list in derived table
{
Public
C () {cout<< "C" <<ENDL;}
~c () {cout<< "~c" <<endl;}
Private
b b;
Public
A;
};

int main ()
{
c * p = new C;
Delete p;

System ("PAUSE");
return 0;
}

Results and Analysis:

A//1 constructor for parent Class A
A//2 the constructor of a in parent class B
A//3 member variable B in parent class B (calls the constructor of parent class A)
b//4 member variable B in parent class B (calls the constructor of parent class B)
Construction of member variable B in a//5 C
A
B
The construction of member variable A in a//6 C
C//7 C's constructor Last call (Finally,-__-| | |)
~c
~a
~b
~a
~a
~b
~a
~a
~a

====================================

As you can see, 1~4 is initialized in the order in which it appears in the derived table, calling the constructor of the parent class first

5, 6 constructors that call member variables

7 calling its own constructor

PS: In more complex cases, you can try virtual inheritance.

In cases where virtual inheritance and general inheritance exist, precedence virtual inheritance

For example, Class C:public B, virtual public A

The constructor of a is called first, and then the constructor of B is called.

=============================================================================================================== ==================

http://sun.solrex.org/?p=254

Initialization order of C + + class objects ZZ

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.