The call order of constructors and destructors in c++c++

Source: Internet
Author: User

http://blog.csdn.net/xw13106209/article/details/68993701. References

Reference 1:c++ constructor, destructor call order and dynamic binding of virtual function in inheritance

Reference 2: Invocation time and sequence of calls for constructors, copy constructors, and destructors

Reference 3:c++ constructor and destructor call order

2. Introduction to constructors, destructors, and copy constructors 2.1 constructors

    • Constructors cannot have return values
    • Default constructor, the default constructor is automatically called to initialize the object, and the default constructor initializes all data members to zero or null
    • When you create an object, the system automatically calls the constructor
2.2 Destructors
    • Destructors have no parameters and no return values. cannot be overloaded, which means that only one destructor can be defined in a class
    • If a destructor is not defined in a class, the system automatically generates a default destructor, which is an empty function and does nothing
    • Calling condition: 1. The object defined in the function body, when the function execution ends, the destructor of the object's class is automatically called; 2. An object that is dynamically constructed with the new operator when it is released using the delete operator.
2.3 Copy Constructors

A copy constructor is actually a constructor, with all the attributes of a generic constructor, with the same name as the owning class name. There is only one parameter in the copy constructor, which is a reference to a similar object. It is called in three cases:

    • When a known object of the class is used to initialize another object of the class;
    • The formal parameter of the function is the object of the class, when the function is combined with the parameter and the argument;
    • The return value of the function is the object of the class, and the function returns the caller after execution.
3. The call order object for constructors and destructors is constructed from the "bottom up", when an object is established, the constructor of the base class is called first, and then the constructor of the next derived class is called, and so on until the constructor of the most derived class that derives the most number of derived classes is reached. Because, when the constructor is initially constructed, it always calls the constructor of its base class before it starts executing its constructor body, and when the direct base class constructor is called, the default constructor of the direct base class is called if there is no specific description. When an object is refactored, its order is reversed. 4. Example 14.1 Code
1#include <iostream>2#include <stdio.h>3 using namespacestd;4 class Point5 {6 Private:7     intx, y;//data members8  Public:9Point () {cout <<"Point ()"<<Endl;}TenPointintxx=0,intyy=0)//constructor Function One     { Ax=xx; -y=yy; -cout<<"constructor is called"<<Endl; the     } -Point (Point &p);//copy constructor, parameter is a reference to an object -~point () {cout<<"destructors are called"<<Endl;} -     intGet_x () {returnx;}//Method +     intGet_y () {returny;} - }; +  APoint::p oint (Point &p) at { -x=p.x;//assigns the disguise of the object p to the current member variable.  -y=p.y; -cout<<"The copy constructor is called"<<Endl; - } -  in voidf (point P) - { toCout<<p.get_x () <<"    "<<p.get_y () <<Endl; + } -  thePoint G ()//The return type is point * { $printf"*********%s%d\n", __func__, __line__);Panax NotoginsengPoint A (7, -); -printf"*********%s%d\n", __func__, __line__); the     returnA; + } A  the intMain () + { -Point A ( the, A); $printf"*********%s%d\n", __func__, __line__); $Point B (a);//constructs an object that uses the copy constructor.  -printf"*********%s%d\n", __func__, __line__); -Cout<<b.get_x () <<"    "<<b.get_y () <<Endl; theprintf"*********%s%d\n", __func__, __line__); - f (b);Wuyiprintf"*********%s%d\n", __func__, __line__); theb=g (); -printf"*********%s%d\n", __func__, __line__); WuCout<<b.get_x () <<"    "<<b.get_y () <<Endl; -printf"*********%s%d\n", __func__, __line__); About}
View Code

4.3 Result the analytic constructor is called//point A (15,22);
The copy constructor is called//point B (a); The first invocation of the copy constructor: When you initialize another object of the class with a known object of the class
//cout<<b.get_x () << "" <<b.get_y () <<endl; copy constructor is called//f (b); the second invocation of the copy constructor: the formal parameter of a function is the object of a class, when a function is called to combine formal parameters and arguments
//void f (point P) function outputs the member of Object B
Destructors are called//f (b); The first invocation of a destructor is: An object defined in the body of a function that, at the end of the function execution, is automatically called by the destructor of the class in which the object resides.
The constructor is called//b=g (); The function body point A (7,33); Create Object A
The copy constructor is called//b=g (); The third invocation of the copy constructor, the value of copy A is assigned to B: The return value of the function is the object of the class, and the function returns the caller after execution
Destructors are called//copy constructors corresponding to destructors
Destructors are called//b=g (); The function body Object A is destructor
7 33
destructor is called//destructor of main function body B object
destructor is called//destructor of main function body a object 5. Instance 25.1 Code

1#include <iostream>2 using namespacestd;3 //base class4 classCPerson5 {6     Char*name;//name7     intAge//Age8     Char*add;//Address9  Public:TenCPerson () {cout<<"constructor-cperson!"<<Endl;} One~cperson () {cout<<"deconstructor-cperson!"<<Endl;} A }; -  - //derived classes (Student Class) the classCstudent: PublicCPerson - { -     Char*depart;//The department where students are located -     intGrade//Grade +  Public: -Cstudent () {cout<<"constructor-cstudent!"<<Endl;} +~cstudent () {cout<<"deconstructor-cstudent!"<<Endl;} A }; at  - //derived classes (teacher Class) - //class Cteacher:public CPerson//inherit CPerson class, two-tier structure - classCteacher: PublicCstudent//inherit Cstudent class, three-layer structure - { -     Char*major;//Teacher Professional in     floatSalary//Teacher's salary -  Public: toCteacher () {cout<<"constructor-cteacher!"<<Endl;} +~cteacher () {cout<<"deconstructor-cteacher!"<<Endl;} - }; the  * //Experimental Main program $ intMain ()Panax Notoginseng { - CPerson person; the cstudent student; + cteacher teacher; A}
View Code

5.3 Description in Instance 2, CPerson is the parent class of cstudent, and Cstudent is the parent of Cteacher, then when creating the Cteacher object, first call the base class is the CPerson constructor, and then follow the hierarchy, one layer down.

The call order of constructors and destructors in c++c++

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.