zell
zell

Reputation: 10204

Understanding behavior of the C++ destructor in a derived class

I am reading a piece of code from Stroustrup's C++ programming book.

class Vector_container : public Container {
    Vector v;
public:    
    // Vector_container implements Container
    Vector_container(int s) : v(s) { } // Vector of s elements
    ~Vector_container() {}

    double& operator[](int i) { return v[i]; }
    int size() const { return v.size(); }    
};

The author then says

  1. The destructor (~Vector_container()) overrides the base class destructor (~Container()).
  2. Note that the member destructor (~Vector()) is implicitly invoked by its class’s destructor (~Vector_container()).

Regarding #1, why does the overriding occur in functions of different names?

Regarding #2, is it a C++ feature that member destructors are systematically invoked by the class's destructor?

Upvotes: 2

Views: 186

Answers (4)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

why does the overriding occur in functions of different names?

Because it has to. Otherwise object destruction would not work. The base part of the object must be destructed too, so this is how it works.

The syntax for defining constructors and destructors is a bit confusing, because technically these things do not have names (ref; no specific wording for the dtor but the same applies by deductive reasoning). You can refer to the destructor of a class with the syntax ~<class name> (ref) but that is not, in itself, a function name. This may sound like a totally academic distinction and, well, it is… but it may help to make the "different name overriding" less surprising.

I also wouldn't call this "overriding", a term usually used to describe how virtual functions work. Bjarne is using it loosely.

is it a C++ feature that member destructors are systematically invoked by the class's destructor?

Of course. Imagine if destroying an object did not destroy the members it encapsulates? You'd have to do it yourself, manually, every time. That defeats the very purpose of having automatic scoping.

Upvotes: 1

Klaus
Klaus

Reputation: 25603

Regarding #1, why does the overriding occur in functions of different names?

It is not an override. Each class has its on destructor, maybe user defined or not. And if you destroy an object, each of the destructors are called in the order from bottom to up in the inheritance hierarchy.

Important note: If you have virtual base class and destroy the object by using the base pointer, your destructors must been marked as virtual to also destruct from bottom to top and not only the type which the base pointer have.

Regarding #2, is it a C++ feature that member destructors are systematically invoked by the class's destructor?

Yes, the destructor calls the destructors from all members.

Hint: If the member is a raw pointer, the destructor do only "destruct" the pointer, not the element it points to. As this, you should either use smart pointers or take care to delete all objects your self in your user provided destructor which you have created somewhere else.

Upvotes: 2

GaryO
GaryO

Reputation: 6338

The compiler creates a destructor if you don't add one. In your case you have one, so the compiler doesn't generate one.

And yes, members and base classes are destructed after the class's destructor is called. From cppreference.com:

For both user-defined or implicitly-defined destructors, after the body of the destructor is executed, the compiler calls the destructors for all non-static non-variant members of the class, in reverse order of declaration, then it calls the destructors of all direct non-virtual base classes in reverse order of construction (which in turn call the destructors of their members and their base classes, etc), and then, if this object is of most-derived class, it calls the destructors of all virtual bases.

Upvotes: 3

Renat
Renat

Reputation: 8962

1: Destructor is a special function, the name doesn't matter here.

2: Yes, it's a C++ feature. Since v member declared as Vector v, container's destructor will call its member's destructor Vector::~Vector automatically

Upvotes: 2

Related Questions