Why is the virtual destructor in the derived class empty?

I have one question, I see that in some codes the virtual destructor in the derived class is empty, then why do we need it since it does nothing? Is it used to call the destructor in the base class and without it, the destructor cannot be called? or is it just an instruction to tell the reader of the code that this destructor is virtual, making the code much easier to read? Thank you!

+3


source to share


1 answer


why do we need this, because it does nothing?

If the base class already has a virtual destructor, then we don't need to define an empty one in the derived class. The only reason for this is to remind other readers of your code that there is a virtual destructor for the class.



Is it used to call the destructor in the base class and without it, the destructor cannot be called?

An empty destructor participates in the destructor chain in the same way as a non-empty or inherited destructor. As long as the base class has a virtual destructor, providing an empty override does not change behavior.

+6


source







All Articles