The construction and destruction Order of C + + class members

Source: Internet
Author: User

I think the object-oriented children's shoes should not be unfamiliar with classes and objects!

The object is not suddenly built, and the object must be created at the same time as the parent class and the object contained therein. C + + follows the following creation order:

(1) If a class is specific to a base class, the default constructor for the base class is executed.

(2) Non-static data members of the class, created in the order in which they are declared.

(3) Executes the constructor of the class.

When a class is constructed, its parent class is constructed, and then the class member is created, and finally the constructor of itself is called.

Let's look at an example below.

Class C{public:    C () {printf ("c\n");} Protected:private:};class B {public:    B () {printf ("b\n");} Protected:    C c;private:};class a:public b{public:    A () {printf ("a\n");} Protected:private:};int Main () {    a A;    GetChar ();}

The result of this example is:

Let's analyze it. The first defines 3 classes a B C, where a inherits from B, constructs a in the main function, because A is inherited B, so B is constructed first, and then Class B has a member Class C, so the Class C is first constructed, then B, and finally a.

Looking at an example, the above should be a bit:

Class C{public:    C () {printf ("c\n");} Protected:private:};class B {public:    B () {printf ("b\n");} Protected:private:};class a:public b{public:    A () {printf ("a\n");} Protected:    C C;private:};int Main () {    a A;    GetChar ();}

It doesn't change much, just adds a C member to a, and B removes it.

The result is:

Also constructs a in main, a inherits from B, so first constructs B, then constructs a data member C of itself, and finally calls the constructor of a itself:

You should understand the details of the construction here.

Next look at the sequence of destructors:

(1) Call the destructor of the class.

(2) Destroying data members, in contrast to the order in which they were created.

(3) If there is a parent class, call the parent class's destructor.

Let's look at an example:

Class C{public:    C () {}    ~c () {printf ("c\n");} Protected:private:};class B {public:    B () {}    ~b () {printf ("b\n");} Protected:private:};class a:public b{public:    A () {}    ~a () {printf ("a\n");} Protected:    C C;private:};int Main () {    a A;    return 0;}

The result is:

The process is that at the end of the main function will destroy a, will first call a destructor, successively destroy A's data member C, and finally destroy A's parent class B. In fact, the order of creation is reversed.

The construction and destruction Order of C + + class members

Related Article

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.