Home > Backend Development > C++ > What's the Exact Call Order of Constructors and Destructors in Inheritance?

What's the Exact Call Order of Constructors and Destructors in Inheritance?

Linda Hamilton
Release: 2024-12-03 02:34:10
Original
370 people have browsed it

What's the Exact Call Order of Constructors and Destructors in Inheritance?

Understanding Constructor and Destructor Call Order in Inheritance

In object-oriented programming, classes can inherit from base classes, creating a hierarchical relationship where the derived class inherits the properties and behaviors of the parent class. When creating and destroying objects of these classes, the order of constructor and destructor calls plays a crucial role in initializing and finalizing the objects.

For the given example:

struct A {
    A() { cout << "A() C-tor" << endl; }
    ~A() { cout << "~A() D-tor" << endl; }
};

struct B : public A {
    B() { cout << "B() C-tor" << endl; }
    ~B() { cout << "~B() D-tor" << endl; }

    A a;  // Field of type A in class B
};
Copy after login

And the following code in main:

int main() {
    B b;
}
Copy after login

Construction Order

  1. Base Class Construction: The construction process always begins with the base class constructor. In this case, A is the base class, so its constructor A() is called first.
  2. Member Field Construction: After the base class is constructed, the member fields are initialized in the order they are declared. Here, B has a field a of type A. Since there is no initializer list explicitly defined, a default initializer is used, which simply calls the constructor for a.
  3. Derived Class Construction: Once the member fields are initialized, the derived class constructor can be called. In this case, B() is called.

Destruction Order

The order of destructor calls is exactly the reverse of the construction order:

  1. Derived Class Destruction: First, the derived class destructor ~B() is called.
  2. Member Field Destruction: Next, the member fields are destructed in reverse order of their declaration. The field a of type A will be destructed.
  3. Base Class Destruction: Finally, the base class destructor ~A() is called.

Irrespective of whether an initializer list is used or not, the call order for construction and destruction will follow these principles, ensuring proper initialization and cleanup of objects in the inheritance hierarchy.

The above is the detailed content of What's the Exact Call Order of Constructors and Destructors in Inheritance?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template