Do I Need to Explicitly Call the Base Virtual Destructor

Do I need to explicitly call the base virtual destructor?

No, destructors are called automatically in the reverse order of construction. (Base classes last). Do not call base class destructors.

need to call the base destructor method from a derived class in c++?

The base class destructor is automatically invoked in this case; you do not need to call it.

However, note that when destroying an object through delete on a base class pointer and the destructor is not virtual, the result is going to be undefined behavior (although you might not get a crash).

Always declare the destructor as virtual in any class which is meant to be derived from. If the base class does not need to have a destructor, include a virtual one anyway with an empty body.

There is an exception to the above rule for an edge case: if your derived classes do not need to support polymorphic destruction, then the destructor does not need to be virtual. In this case it would be correct to make it protected instead; more details here, but be advised that this rarely occurs in practice.

Is it legal to explicitly call base class destructor/constructor?

No, this is not legal. You're not allowed to replace the base subobject of an object.

C++11 3.8/7 specifies that you can only reuse an object's storage if

the original object was a most derived object (1.8) of type T and the new object is a most derived object of type T (that is, they are not base class subobjects).

The object you replace was a base class subobject, not a most derived object, and so is forbidden.

If you were to replace the entire object (i.e. call ~C, then construct a new C), then that would be legal, but dangerous. If the constructor threw, then the object would be destroyed a second time at the end of its lifetime. That would give undefined behaviour.

Virtual destructor needed for class which is both derived and base?


Is ~B() virtual because I declared ~A() with the virtual function specifier?

Yes. Per https://timsong-cpp.github.io/cppwp/n4659/class.dtor#10:

If a class has a base class with a virtual destructor, its destructor (whether user- or implicitly-declared) is virtual.

With virtual destructors, do I need to explicitly declare a virtual destructor for each subclass?

The default destructor is not virtual. If you declare the destructor of your base class as virtual, the destructors of the subclasses will be overrides, and thus also be virtual even without explicitly declaring them to be.

The GNU GCC compiler even gives a warning if you have a class hierarchy and your base class does not declare the destructor to be virtual because you most likely want it to be.

Do I still need to have a virtual destructor for my derived class in this situation?

It already is!

The virtual keyword is implicit for your derived::someFunc, and for your derived::~derived, because both functions' base equivalents are marked virtual.

So, you may write the keyword virtual on both, neither or just one. It doesn't matter: they will both be actually virtual regardless.

Do I need a virtual destructor if descendant classes have no non-static members or destructors?

If you delete derived classes via pointers to base classes then the behavior will be undefined without a virtual destructor, no matter how the derived classes look.

C++11 Standard, §5.3.5/3:

If the static type of the object to be deleted is different from its
dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined.

However, if the classes differ only in their constructor, consider using alternatives to derivation, e.g. simple free functions like create_named_file().

Do I need to define a virtual destructor even if the base and derived class only use primitive data types?

That statement from the standard you quote means that a sample like (using your class B and D)

int main()
{
B *object = new D;
delete object;
}

has undefined behaviour if B does not have a virtual destructor.

No exceptions to that rule.

It doesn't matter what the classes (or their member functions) do or don't do. It is the non-virtual destructor in B that causes the delete expression (delete object) to have undefined behaviour. No exceptions.



Related Topics



Leave a reply



Submit