Search code examples
c++destructorobject-lifetime

What does a implicitly defined destructor do


What does the implicitly defined destructor do? Is it just an empty function that is defined by the compiler?

struct Foo
{
  int i;
};

struct Bar
{
  int i;
  ~Bar()
  { 
    // empty...
  }
};

Is the destruction of Foo identical to Bar? Or does the implicit destructor do something inside of the compiler generated body?


Solution

  • What does the implicitly defined destructor do?

    It will be the same as an explicitly defined destructor with an empty body. In effect, it destroys all sub-objects and does nothing more.

    Is it just an empty function that is defined by the compiler?

    In practice, there may not even need to be an empty function. But it may be useful to to think that there is when thinking in terms of the abstract machine.

    Or does the implicit destructor do something inside of the compiler generated body?

    Depends on the class. In the case of Foo, nothing needs to be done by the destructor.