What is the use of virtual destructor?

Showing Answers 1 - 9 of 9 Answers

Confused? Here's a simplified rule of thumb that usually protects you and usually doesn't cost you anything: make your destructor virtual if your class has any virtual functions. Rationale:

  • that usually protects you because most base classes have at least one virtual function.
  • that usually doesn't cost you anything because there is no added per-object space-cost for the second or subsequent virtual in your class. In other words, you've already paid all the per-object space-cost that you'll ever pay once you add the first virtual function, so the virtual destructor doesn't add any additional per-object space cost. (Everything in this bullet is theoretically compiler-specific, but in practice it will be valid on almost all compilers.)

  Was this answer useful?  Yes

sphurti

  • Oct 9th, 2005
 

It is legal and common to pass a pointer to a derived object when a pointer to a base object is expected.

What happens when the pointer to the derived object is deleted?

If the destructor is virtual is virtual

Bcoz the derived class destructor wll automatically invoke the base's class destructor the entire object will be properly deleted.

  Was this answer useful?  Yes

Deepu Abraham K

  • Oct 19th, 2005
 

Its always better to have a  virtual destuctor in a class which has got virtual functions.When an object is created with instantiating the derived class like [baseclass* bclass = new derivedclass] when you delete the base class pointer it calls the derived calss destructor also so it leaves no chance for memory leak.

  Was this answer useful?  Yes

Its always better to have a  virtual destuctor in a class which has got virtual functions.When an object is created with instantiating the derived class like [baseclass* bclass = new derivedclass] when you delete the base class pointer it calls the derived calss destructor also so it leaves no chance for memory leak.

  Was this answer useful?  Yes

bappa

  • Jun 6th, 2006
 

virtual destructor is very useful....everyone should use that......if there is no any strong reason for not using virtual destructor....like...One class having two char variable...........so it's size is two byte........if u use virtual destructor it's size will be 6 bytes....4 byte for virtual ptr....Now if this class have 1 millions objects...so 4 magabyte memory will be lost...where all ptr do the same thing.....

  Was this answer useful?  Yes

baikunta

  • Aug 30th, 2006
 

really when we delete an object through base class pointer at that time virtual destrocter most for virtual function for order base des-derived-des for avid memory leakbut its depend upon u when u delete the object through derived class object its depend upon usearmost of thing is that dep. upon user........

  Was this answer useful?  Yes

Arindam

  • Nov 27th, 2006
 

these are the following reasons to use virtual destructors:-

1.  Without a virtual destructor, the proper destructor may not be
called:

struct B {~B();};
struct D : B {~D();};
B* b = new D;
delete b; // <--------- Will not call D::~D() !!!!!

2. Without a virtual destructor, operator delete(void*, size_t) may
not be called with the correct size.

struct B {~B(); operator delete(void*, size_t);};
struct D : B {~D();};
B* b = new D;
delete b; // <--------- Will call operator delete(void*, size_t) with
          //            the size of B not the size of D!!!

3.  Without a virtual destructor, and when MI is used, operator
delete(void*) or operator delete (void*, size_t) may be called with
the wrong address.

struct B {~B();};
struct A {};
struct D : A, B {~D();};
B* b = new D;
delete b; // <--------- May not pass to operator delete the address
          //            that was returned by operator new!!!


Thank u...it may help u...

  Was this answer useful?  Yes

A virtual destructor has the keyword virtual in it. If you destroy an object through a caller or reference to a base class, and the base class destructor is not virtual, the derived class destructors are not executed and the destruction is not complete which might lead to memory leaks. 

  Was this answer useful?  Yes

rimsha

  • Jul 27th, 2011
 

virtual destructor is used to delete the dynamically allocated memory via a base class ptr.

  Was this answer useful?  Yes

You should always keep your destructors virtual when u r dealing with Inheritance.

Try this Code :

Code
  1. #include <iostream>

  2. #include <tchar.h>

  3.  

  4. using namespace std;

  5.  

  6. class Parent

  7. {

  8. public:

  9.         virtual ~Parent()

  10.         {

  11.                 cout << " Destroying Parent

  12. ";

  13.         }

  14. };

  15.  

  16. class Child : public Parent

  17. {

  18. public:

  19.         ~Child()

  20.         {

  21.                 cout << " Destroying Child

  22. ";

  23.         }

  24. };

  25.  

  26.  

  27. int _tmain(int argc, _TCHAR* argv[])

  28. {

  29.         Parent * abc = new Child();

  30.         delete (abc);

  31.  

  32.         return 0;

  33. }

  34.  



Also try the same code without using virtual keyword before ~Parent()
u will see the difference... :)

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions