Execution Process of C ++ constructor destructor

Source: Internet
Author: User
Constructor:
C ++ provides constructor to process object initialization. It is automatically executed when an object is created. The name of the constructor must be the same as the class name. It does not have any type or return any value. The function of the constructor is defined by the user. The user designs the function body and function parameters according to the initialization requirements. The constructor is called when the class object enters its scope. The constructor does not return values, so it does not need to declare the type when defining the constructor. This is an important difference between the constructor and the general function.

If the constructor does not contain parameters, assign the initial values to the data member in the function body so that each object of the class can obtain the same group of initial values. A constructor with parameters is used. When calling constructor of different objects, different data is transmitted from the outside to the constructor for different initialization. The common format of the constructor header is the constructor name (type 1 parameter 1, type 2 parameter 2 ,...)

Destructor:
Destructor is also a special member function, which is opposite to the constructor. Its name is preceded by a Class Name "~". Symbol. destructor are the opposite of constructors. When the object's life cycle ends, the Destructor is automatically executed: ① if an object is defined in a function (it is an automatic local object), when the function is called, the object should be released. The Destructor is automatically executed before the object is released. ② When a static local object ends a function call, the object is not released, so the Destructor is not called. Only when the main function ends or the exit function ends the program, to call the destructor of a static local object. ③ If a global object is defined, the destructor of the global object will be called when the process of the program leaves its scope (for example, the main function ends or the exit function is called. ④ If an object is dynamically created using the new operator, when the object is released using the delete operator, the destructor of the object is called first.

The role of the Destructor is not to delete objects, but to clear the memory occupied by objects before they are cleared, so that the memory can be allocated to new objects by the program. The Destructor does not return any value, no function type, or function parameters. Therefore, it cannot be overloaded. A class can have multiple constructors, but only one destructor. Of course, destructor can also be used to execute "any operation that the user wants to perform after the last time the object is used", for example, output related information. If no destructor is defined, the C ++ compilation system automatically generates a destructor. In fact, no operations are performed.

In general, the order of calling the Destructor is the opposite of that of calling the constructor: The first called constructor, which corresponds to (in the same object) the Destructor is called at the end, and the constructor called at the end is called at the first time.


(1) The object defined in the global scope (that is, the object defined outside all functions), its constructor is called before all functions (including the main function) in the file are executed. However, if a program contains multiple files and different files define global objects, the execution sequence of the constructor of these objects is uncertain. When the main function is executed or the exit function is called (the program is terminated at this time), The Destructor is called. (2) If a partial Automatic Object is defined (for example, an object defined in a function), its constructor is called when an object is created. If a function is called multiple times, the constructor is called every time an object is created. The Destructor is called at the end of the function call and when the object is released. (3) If a static local object is defined in a function, the constructor is called only once when the program calls this function for the first time to create an object. The object is not released at the end of the call, therefore, you do not call the destructor. The Destructor is called only when the main function ends or the exit function ends.
Basic destructor are usually virtual functions.

A class has only one destructor.

When creating a derived class object: base class constructor → constructor of the derived class;

1. If the parent class constructor is explicitly called in the constructor of the derived class, it must be written to the initialization parameter table.

2. If the parent class constructor is not explicitly called in the constructor of the derived class, the constructor without a function is called by default.

3. If the parent class constructor is not explicitly called in the constructor of the derived class, no constructor is defined in the parent class or a constructor must have no parameters.

 

 

The object of the derived class is: The destructor of the derived class → the destructor of the base class.

1. If you use a parent class pointer to a derived class object, the destructor of the parent class must be declared as virtual; otherwise, the destructor of the derived class cannot be called.

2. Call the constructor of the base class with the correct parameters as part of the constructor of each derived class (discussed)
3. Common member functions cannot use this syntax to call basic class methods.
4. A class is only responsible for the construction of its direct base class. However, there are different virtual base classes.
5. The parameters of the derived class constructor should include the parameters you need to use and the basic class.
6. The derived class inherits the behavior and structure of the Base class, but does not inherit the constructor and destructor.
7. You need to call the copy constructor of the base class in the copy constructor of the derived class.
8. You need to call the assignment operator of the base class in the assignment operator of the derived class.
9. The derived class destructor do not explicitly call the basic class destructor.
10. The used virtual base class is initialized by the constructor of the final derived class. When an object is created, calls to the virtual base class constructor within the sub-object constructor are ignored.
11. public inheritance is the main mode of inheritance. Private inheritance is only used in special cases (for example, stack classes can be inherited from List classes, but it is not a list, re-export the members of the private base class .) Private inheritance has no polymorphism.
12. Do not use multiple inheritance when a single inheritance can achieve the goal
13. inherited advantages: code reuse, adding new classes and new features (such as satellite and patient monitoring), dynamic Association, and Polymorphism to running programs.
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.