-2

I have a class (the "parent") which has as a member another class (the "child"):

class Engine   // THE "PARENT" CLASS
{
    public:
        // CONSTRUCTOR ------------------------------------
        Engine() : mConfig {}  // "CHILD" CLASS INITIALIZED HERE
        {
            // ...
            return;
        }

        // DESTRUCTOR -------------------------------------
        ~Engine()
        {
            std::cout << "Engine destructor executing...\n";
            // ...
            return;
        }

    private:
        GameConfig  mConfig;  // THE "CHILD" CLASS AS MEMBER OF "PARENT"
};

The "child" class is defined as:

class GameConfig   // THE "CHILD" CLASS DEFINITION
{
    public:
        // ...
        // DESTRUCTOR -------------------------------------
        ~GameConfig()
        {
            std::cout << "Writing config data...\n";
            // ...
            return;
        }
};

I instantiate the "parent" in main, which in turn (via initializer list) instantiates the "child" class:

int main()
{
    Engine gameEngine {};
    // ...
    std::cout << "Goodbye!\n";

    return(0);
}

Based on this question asked on SO, as well as logically, I would have thought the "child" object's destructor would be called before the "parent" object's destructor. But, when I execute this code, the output is:

Goodbye!
Engine destructor executing...
Writing config data...

So the question is: why does a "child" object destructor execute after the "parent" object destructor?

  • [The order of destruction is well-defined.](https://en.cppreference.com/w/cpp/language/destructor#Destruction_sequence). – Some programmer dude Apr 03 '21 at 19:24
  • 5
    If members were destroyed before entering the containing class's destructor, the containing class's destructor would not be able to do anything with them. – jamesdlin Apr 03 '21 at 19:27
  • Where is the parent-child relationship in the code? – Konjit W Apr 03 '21 at 19:44
  • @jamesdlin I see, that makes it pretty clear as to why. Seems obvious for non-beginners, and in hindsight, of course. Can I ask why not place your comment as an answer? –  Apr 03 '21 at 19:55
  • @Someprogrammerdude That article does explain the order of destruction, however it does not explain the why, which was my question –  Apr 03 '21 at 22:10

1 Answers1

1

(I'm going to avoid using "parent" and "child" since those terms typically refer to classes that are related by inheritance, not by composition.)

Destroying a C++ object involves executing its destructor's body and additional destruction work that is automatically generated by the compiler, such as destroying member objects and invoking base class destructors.

That additional work must happen after the the destructor's body is invoked. If it instead happened before, the containing object's destructor would not be able to do anything with its members, which in most cases would make the destructor useless.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • Understood regarding the "child" and "parent" terms. I lacked any better terminology, hence why I placed those words in quotes throughout my question. Seems I get punished often on SO for not already knowing what I'm trying to learn. Thanks for the explanation. –  Apr 03 '21 at 22:09