Understanding of constructors and destructors for derived classes in C + +

Source: Internet
Author: User

One: constructor function

Form: Derived class Name:: Derived class Name: base class name 1 (parameter 1), base class Name 2 (Parameter 2), ... base class name N (parameter n), data member 1 (parameter 1), data member 2 (parameter 2), ... data member n (parameter n) {

Description of the various operations


Execution procedure: First executes the constructor of the base class, then assigns the data member, finally executes the function body.

The order in which the base class name and data members are executed is determined by the order in which they are declared in the definition of the derived class, so their order is arbitrary, but for readability it is best to write sequentially.

If the base class has only a default constructor, the base class name and parameter table can be used without writing.

Two: copy constructor

The form of a constructor for a derived class is illustrated with an example:

Derived::D erived (derived&v): Base1 (v) {...}

Careful words will find that the formal parameters of Base1 and derived are the same as C, where the type compatibility rules are used.

Three: Destructors

The form of the destructor of the derived class and the destructor of the ordinary class are identical in form, and the order of execution and the constructor of the derived class are executed exactly the opposite, that is, the function body is executed first, then the destructor of the member is executed, and the destructor of the base class is executed finally.

This is my understanding of the constructors and destructors of derived classes.

Understanding of constructors and destructors for derived classes in 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.