Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

order of destruction using virtual

Can some one please help what the order of destruction is when I am using virtual functions. Does it start with the base class and then derived class?

like image 396
brett Avatar asked Aug 17 '10 21:08

brett


People also ask

How does a virtual destructor work?

In simple terms, a virtual destructor ensures that when derived subclasses go out of scope or are deleted the order of destruction of each class in a hierarchy is carried out correctly. If the destruction order of the class objects is incorrect, in can lead to what is known as a memory leak.

Can destructors be created virtual?

Yes, it is possible to have a pure virtual destructor. Pure virtual destructors are legal in standard C++ and one of the most important things to remember is that if a class contains a pure virtual destructor, it must provide a function body for the pure virtual destructor.

What is virtual destruction?

A virtual destructor is used to free up the memory space allocated by the derived class object or instance while deleting instances of the derived class using a base class pointer object.

Why do destructors need to be virtual?

Virtual destructors in C++ are used to avoid memory leaks especially when your class contains unmanaged code, i.e., contains pointers or object handles to files, databases or other external objects. A destructor can be virtual.


1 Answers

Since I don't see how virtual function change any objects' destruction order, I assume you're referring to the order of destruction for base classes and data members in a virtual inheritance scenario.

Sub-objects are constructed

  1. base classes are constructed from most base to most derived;
  2. multiple base classes are constructed in the order of their declaration as base classes;
  3. virtual base classes are constructed before all others, amongst themselves adhering to the above two rules;
  4. data members are constructed before the enclosing object's constructor's body is executed, in order of their declaration.

Destruction is simply the opposite of construction, so you only need to memorize the above.

However, the above four rules are in that order because that makes sense, and if you understand why this order makes sense, you will not even have to memorize those four rules, but can infer them from your understanding (as I just did). So let's examine that order:

  • You might want to use whatever service the base class provide from a derived class' constructor. Of course, you cannot use a (base) class object before it's actually constructed. Therefore, when a derived class is constructed, the base class needs to be already constructed. (Incidentally, this also explains why the virtual function dispatching doesn't fully work from within constructors: When a sub-object is constructed, only the sub-objects of base classes are already constructed; the derived classes' sub-objects are not yet constructed. Therefore a call to a virtual function must not be dispatched to a derived class. As always, destructors are the same, just backwards.)
  • With multiple base classes being equal siblings, some order had to be picked arbitrarily. Ultimately, the order of declaration is the most simple one to use. Data members, which also are equal siblings, follow the same (more or less arbitrary) in-order-of-declaration rule.
  • Virtual base classes are strange beasts. Because there will always only be one sub-object of a virtual base class, there's that special rule which says it always needs to be constructed first, right from the most derived class' constructor. (Which is why virtual base classes work best as abstract base classes with no data and only default constructors.)
like image 93
sbi Avatar answered Oct 20 '22 20:10

sbi