-1

I have a class structure of three classes where two of them are base classes of the third, like this:

class A {
};
class B {
};
class C : public A, public B {
};

When an instance of C is to be destroyed, in which order are the base classes A and B destroyed? Are there any rules for this?

Atle
  • 1,867
  • 12
  • 10
  • 3
    This has to be a dupe. I can't believe it hasn't been asked before. – Tim Seguine Dec 07 '13 at 16:00
  • Maybe it has been asked before only with another formulation/different words. – Atle Dec 07 '13 at 16:04
  • doesn't this question have your answer in it? http://stackoverflow.com/questions/2254263/order-of-member-constructor-and-destructor-calls – Tim Seguine Dec 07 '13 at 16:10
  • http://stackoverflow.com/questions/7539282/order-of-calling-constructors-destructors-in-inheritance is closer, @tim. – Mat Dec 07 '13 at 16:11
  • @Mat agreed. If it was that easy to find though, I would expect a user with over 1k rep to look a litte harder before asking. – Tim Seguine Dec 07 '13 at 16:12
  • @Tim, I didn't find any questions here of this exact problem with two base classes. I see now that the answer in the question linked in from Mat answers my question about base class destruction order even when the questioner didn't ask about that. Btw., i do Google a lot before asking, this is my second question ever. – Atle Dec 07 '13 at 16:22

1 Answers1

5

There are rules (C++11 §12.4):

After executing the body of the destructor and destroying any automatic objects allocated within the body, a destructor for class X calls the destructors for X’s direct non-variant non-static data members, the destructors for X’s direct base classes and, if X is the type of the most derived class (12.6.2), its destructor calls the destructors for X’s virtual base classes. All destructors are called as if they were referenced with a qualified name, that is, ignoring any possible virtual overriding destructors in more derived classes. Bases and members are destroyed in the reverse order of the completion of their constructor (see 12.6.2). A return statement (6.6.3) in a destructor might not directly return to the caller; before transferring control to the caller, the destructors for the members and bases are called. Destructors for elements of an array are called in reverse order of their construction (see 12.6).

The construction order is (§12.6.2/10):

In a non-delegating constructor, initialization proceeds in the following order:

— First, and only for the constructor of the most derived class (1.8), virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.
— Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
— Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
— Finally, the compound-statement of the constructor body is executed.

[ Note: The declaration order is mandated to ensure that base and member subobjects are destroyed in the reverse order of initialization. — end note ]

So in simple cases, base constructors are called in the order in which the classes are listed in the declaration, and destructors run in reverse of that order.

Mat
  • 202,337
  • 40
  • 393
  • 406