GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 57 of 222    Print  
what is the use of virtual destructor?

  
Total Answers and Comments: 9 Last Update: March 16, 2009     Asked by: Pravesh Kumar 
  
 Sponsored Links

 

 

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
October 06, 2005 07:14:35   
moniruddinahammed Member Since: October 2005   Contribution: 2    

RE: what is the use of virtual destructor?

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.)

 
Is this answer useful? Yes | No
October 09, 2005 14:40:43   
sphurti        

RE: what is the use of virtual destructor?

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.


 
Is this answer useful? Yes | No
October 19, 2005 11:34:43   
Deepu Abraham K        

RE: what is the use of virtual destructor?

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.


 
Is this answer useful? Yes | No
November 05, 2005 06:30:03   
vaniparuchuri Member Since: November 2005   Contribution: 2    

what is the use of virtual destructor?
How can i get the answer?
 
Is this answer useful? Yes | No
November 05, 2005 06:41:36   
vaniparuchuri Member Since: November 2005   Contribution: 2    

RE: what is the use of virtual destructor?

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.


 
Is this answer useful? Yes | No
June 06, 2006 05:15:57   
bappa        

RE: what is the use of virtual destructor?

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.....


 
Is this answer useful? Yes | No
August 30, 2006 03:25:26   
baikunta        

RE: what is the use of virtual destructor?
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........
 
Is this answer useful? Yes | No
November 27, 2006 23:01:50   
Arindam        

RE: what is the use of virtual destructor?

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...


 
Is this answer useful? Yes | No
March 16, 2009 07:48:13   
shekharsp1 Member Since: March 2009   Contribution: 1    

RE: what is the use of virtual destructor?
This used to delete the dynamically allocated memory from derived as well as base class
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 

 

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2010 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape