Call Sequence of constructor and destructor in C ++

Source: Internet
Author: User
Document directory
  • 2.2 destructor
  • 2.3 copy constructor
  • Code 4.1
  • 4.2 running results
  • Code 5.1
  • 5.2 running results
  • 5.3 Description
1. References

Reference 1:
C ++ inherits the calling sequence of constructors, destructor, and dynamic binding of virtual functions

Reference 2: Call time and call sequence of constructor, copy constructor, and destructor

Reference 3: Calling sequence of C ++ constructor and destructor

2. constructor, destructor, and copy constructor introduction 2.1 Constructor

  • The constructor cannot return values.
  • When the default constructor is used, the system automatically calls the default constructor to initialize the object. The default constructor initializes all data members to zero or empty.
  • When an object is created, the system automatically calls the constructor.
2.2 destructor
  • The Destructor has no parameters or return values. It cannot be overloaded. That is to say, only one destructor can be defined in a class.
  • If no destructor is defined in a class, the system automatically generates a default destructor, which is an empty function and does nothing.
  • Call conditions: 1. an object defined in the function body. When the function execution ends, the destructor of the class of the object will be automatically called. 2. an object dynamically built using the new operator is released using the delete operator.
2.3 copy constructor

A copy constructor is actually a constructor. It has all the features of a General constructor, and its name is the same as its class name. The copy constructor has only one parameter, which is a reference to a similar object. It is called in three cases:

  • When initializing another object of the class with a known object of the class;
  • The parameter of a function is a Class Object. When a function is called to combine the parameter with an actual parameter;
  • The return value of a function is a Class Object. After the function is executed, the caller is returned.
3. the Calling sequence objects of constructor and destructor are constructed from the bottom up. When an object is created, the base class constructor is called first, then, call the constructor of the next derived class, and so on until it reaches the constructor of the class with the most derivation times. The constructor always calls the constructor of its base class before executing the constructor, if no special description is provided, the default constructor of the direct base class is called. In object structure, the order is the opposite. 4. instance code 14.1
# Include <iostream> using namespace STD; Class Point {PRIVATE: int X, Y; // data member public: Point (INT xx = 0, int YY = 0) // constructor {x = xx; y = YY; cout <"constructor called" <Endl ;}point (point & P); // copy constructor, parameters are references to objects ~ Point () {cout <"destructor called" <Endl ;}int get_x () {return x ;}// method int get_y () {return y ;}; point: Point (point & P) {x = P. x; // assign the disguised value of object P to the current member variable. Y = P. y; cout <"copy constructor called" <Endl;} void F (point P) {cout <p. get_x () <"" <p. get_y () <Endl;} Point G () // The return type is point {point A (); return a;} void main () {point ); point B (a); // construct an object and use the copy constructor. Cout <B. get_x () <"" <B. get_y () <Endl; F (B); B = g (); cout <B. get_x () <"" <B. get_y () <Endl ;}
4.2 running Result 4.3 result parsing constructor called // point );
The copy constructor is called // point B (a); the first call case of the copy constructor is as follows: When initializing another object of the class with a known object of the class
15 22 // cout <B. get_x () <"" <B. get_y () <Endl; the copy constructor is called // F (B); the second call case of the copy constructor is as follows: Function parameters are class objects. When you call a function to combine the form parameters with the real parameters
15 22 // void F (point P) function outputs the member of object B
The Destructor is called // F (B); the first method of calling the Destructor is as follows: An object defined in the function body. When the function execution ends, the destructor of the class of the object will be automatically called.
The constructor is called // B = g (); point A () in the function body; Create object
The copy constructor is called // B = g (); when the constructor is copied, the value of copy a is assigned to B: The return value of a function is a Class Object. After the function is executed, the caller is returned.
Destructor called // copy the corresponding destructor of the constructor
Destructor called // B = g ();
7 33
The Destructor is called // The destructor of the main function body B object
The Destructor is called // The destructor of the main function body a object 5. instance 25.1 code
# Include <iostream> using namespace STD; // base class cperson {char * Name; // name int age; // age char * Add; // address public: cperson () {cout <"constructor-cperson! "<Endl ;}~ Cperson () {cout <"deconstructor-cperson! "<Endl ;}}; // derived class (Student Class) Class cstudent: Public cperson {char * depart; // The Int grade of the student; // grade public: cstudent () {cout <"constructor-cstudent! "<Endl ;}~ Cstudent () {cout <"deconstructor-cstudent! "<Endl ;}}; // derived class (Textbook class) // class cteacher: Public cperson // inherits the cperson class, and the two-layer structure class cteacher: Public cstudent // inherits the cstudent class, layer-3 Structure {char * Major; // instructor professional float salary; // instructor's salary public: cteacher () {cout <"constructor-cteacher! "<Endl ;}~ Cteacher () {cout <"deconstructor-cteacher! "<Endl ;}}; // void main () {// cperson person; // cstudent student; cteacher teacher ;}
5.2 running Result 5.3 indicates that in instance 2, cperson is the parent class of cstudent, and cstudent is the parent class of cteacher. Therefore, when creating a cteacher object, first, call the base class, that is, the cperson constructor, and then layer by layer.

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.