Invocation times and invocation order of constructors, copy constructors, and destructors

Source: Internet
Author: User

Invocation times and invocation order of constructors, copy constructors, and destructors

Objects are constructed from the bottom up , when an object is created, 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 is always called 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 no specific description is 0.   When an object is refactored, its order is reversed . The following is a brief introduction to these three functions.

constructor function
1. Constructors cannot have return values
2. The default constructor will automatically invoke the default constructor to initialize the object, and the default constructor will have all data members
Initialize to zero or null
3. When you create an object, the system automatically calls the constructor

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.

Copy constructor
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.
!!! is called in three cases:
1. When a known object of the class is used to initialize another object of the class;
2. The formal parameter of the function is the object of the class, when the function is combined with the parameter and the argument;
3. The return value of the function is the object of the class, and the function returns the caller after execution.

Invocation times and invocation order of constructors, copy constructors, and destructors

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.