Calls to constructors and destructors for C + + base classes and derived classes

Source: Internet
Author: User

Calls to constructors and destructors for C + + base classes and derived classes 1. Call Order

When creating an object of a derived class, the system first automatically creates a base class object, that is, before invoking the derived class constructor to create the derived class object, the system first calls the base class's constructor to create the base class object. When the lifetime of the derived class object ends, the destructor for the derived class is called first, and then the destructor of the base class is called.

So, constructors: derived classes, base classes, destructors: derived classes, and base classes.

Example:

1#include <iostream>2 using namespacestd;3 4 classA5 {6  Public:7 A ()8     {9cout <<"Based Class A constructor is called"<<Endl;Ten     } One~A () A     { -cout <<"Based class A destructor is called"<<Endl; -     } the }; - classD | PublicA - { -  Public://do not write public, default is private + B () -     { +cout <<"Derived Class B constructor is called"<<Endl; A     } at~B () -     { -cout <<"Derived class B destructor is called"<<Endl; -     } - }; -  in intMain () - { to b b; +  -     //System ("pause"); If you write this, the VS run result will only show the constructor, not the destructor the     return 0;//put the comment on the sentence, in this sentence plus a breakpoint, manually down one step, you can see the destructor *  $}

"Result of Operation"

2. Implicit invocation and explicit invocation

There are two ways to call the constructor of a base class through the constructor of a derived class: implicit invocation and explicit invocation.

The so-called implicit invocation is that the constructor of the corresponding base class is not specified in the constructor of the derived class, which is called the default constructor of the base class (that is, a constructor that contains default parameter values or no parameters).

The so-called explicit invocation is to specify the constructor of the base class to invoke in the constructor of the derived class, and pass the partial parameter value of the derived class constructor to the base class constructor.

Note: Unless the base class has a default constructor, you must take an explicit call.

2.1 Implicit invocation examples
1#include <iostream>2 using namespacestd;3 4 classA5 {6  Public:7Aintx=0,inty=0)8     {9A = x; b =y;Tencout <<"Based Class A constructor is called"<<Endl; One     } A~A () -     { -cout <<"Based class A destructor is called"<<Endl; the     } - Private: -     intA; -     intb; + }; - //base Class A has a default constructor that you can implicitly invoke + classD | PublicA A { at  Public://do not write public, default is private -Bintz=0)//b (int z) cannot be a default constructor, so no, B () can be a default constructor, so you can also -     { -c =Z; -cout <<"Derived Class B constructor is called"<<Endl; -     } in~B () -     { tocout <<"Derived class B destructor is called"<<Endl; +     } - Private: the     intC; * }; $ intMain ()Panax Notoginseng { - b b; the     //System ("pause"); If you write this, the VS run result will only show the constructor, not the destructor +     return 0;//put the comment on the sentence, in this sentence plus a breakpoint, manually down one step, you can see the destructor A  the}
2.2 Explicitly invoking an example
1#include <iostream>2 using namespacestd;3 4 classA5 {6  Public:7AintXinty)8     {9A = x; b =y;Tencout <<"Based Class A constructor is called"<<Endl; One     } A~A () -     { -cout <<"Based class A destructor is called"<<Endl; the     } - Private: -     intA; -     intb; + }; - //base Class A does not have a default constructor, and its existing constructors need to pass arguments + //when you call A's constructor by using a derived class constructor, you must explicitly call the A classD | PublicA at { -  Public://do not write public, default is private -BintXintYintZ): A (x, y)//b (int z) cannot be a default constructor, so no, B () can be a default constructor, so you can also -     { -c =Z; -cout <<"Derived Class B constructor is called"<<Endl; in     } -~B () to     { +cout <<"Derived class B destructor is called"<<Endl; -     } the Private: *     intC; $ };Panax Notoginseng intMain () - { theb b (1,2,3); +     //System ("pause"); If you write this, the VS run result will only show the constructor, not the destructor A     return 0;//put the comment on the sentence, in this sentence plus a breakpoint, manually down one step, you can see the destructor the  +}

-If there is a wrong place, very welcome to give guidance!

--"Thanks" data from http://www.cnblogs.com/krisdy/archive/2009/06/11/1501390.html

Calls to constructors and destructors for C + + base classes and derived classes

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.