virtual destructor

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello everyone,


What is the purpose of virtual destructor? If currently, no derived class?


thanks in advance,
George
 
George said:
Hello everyone,


What is the purpose of virtual destructor? If currently, no derived class?

Guarantees that if a derived class is ever defined, it can be destroyed
polymorphically.

Virtual destructors aren't needed in every circumstance, but it's a good
idea to use one if in doubt, because the cost is small (the memory manager
cost to destroy an object is larger than the cost of the indirect function
call). However, if you haven't any other virtual functions, your object
can't be used polymorphically and you don't need a virtual destructor.
 
Thanks Ben,


I think you mean if the destructor is virtual, then if we delete an object
of derived class through based class pointer, then the derived class
destructor is invoked? Right?

If the destructor is non-virtual, then if we delete an object of derived
class through based class pointer, then the base class destructor is invoked?
Right?


regards,
George
 
George said:
Thanks Ben,


I think you mean if the destructor is virtual, then if we delete an object
of derived class through based class pointer, then the derived class
destructor is invoked? Right?

Correct. Base class destructors will be automatically invoked in the
correct order as well according to the object lifetime rules.
If the destructor is non-virtual, then if we delete an object of derived
class through based class pointer, then the base class destructor is
invoked?
Right?

That's what will most likely happen in real life, but I think the standard
simply says "undefined behavior". Object lifetime rules are being violated
and the implementation is allowed to do anything it wants, including being
totally inconsistent.
 
Thanks Ben,

Correct. Base class destructors will be automatically invoked in the
correct order as well according to the object lifetime rules.

You mean if the destructors are virtual, even if they do not have explicit
invocation relationship in code (e.g. the most derived destructor will call
the second-last destructor explicitly in source codes), the second-last
destructor, the third-last destructor and so forth will be invoked
automatically?

(If we delete an object with a virtual destructor, through a base instance,
the most derived destructor will be called first, then the second-last
destructor, the third-last destructor and so froth -- right up to the base
destructor. All are automatically invoked without invocation relationship
explicitly in source codes)

But if the destructor is not virtual, we do not have this feature?


regards,
George
 
George said:
Thanks Ben,



You mean if the destructors are virtual, even if they do not have explicit
invocation relationship in code (e.g. the most derived destructor will call
the second-last destructor explicitly in source codes), the second-last
destructor, the third-last destructor and so forth will be invoked
automatically?

(If we delete an object with a virtual destructor, through a base instance,
the most derived destructor will be called first, then the second-last
destructor, the third-last destructor and so froth -- right up to the base
destructor. All are automatically invoked without invocation relationship
explicitly in source codes)

But if the destructor is not virtual, we do not have this feature?

George:

No. All destructors have this feature, even when they are not virtual.
But having a virtual destructor ensures that the process always starts
with the most derived constructor.
 
Thanks David,


regards,
George

David Wilkinson said:
George:

No. All destructors have this feature, even when they are not virtual.
But having a virtual destructor ensures that the process always starts
with the most derived constructor.
 
Back
Top