IT Community - Software Programming, Web Development and Technical Support

Explain the need for virtual destructor

This is a discussion on Explain the need for virtual destructor within the C and C++ Programming forums, part of the Software Development category; Hi, can anyone tell need for virtual destructor. Thanks, Prasath.K...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C and C++ Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-17-2007, 05:45 AM
prasath prasath is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: Chennai
Posts: 173
prasath is on a distinguished road
Default Explain the need for virtual destructor

Hi,
can anyone tell need for virtual destructor.


Thanks,
Prasath.K
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-17-2007, 06:01 AM
Senthilkumar Senthilkumar is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 93
Senthilkumar is on a distinguished road
Default Re: Explain the need for virtual destructor

When classes are inherited, we need to make the base class destructor virtual – to make sure when the object is destroyed, all the derived class destructors also called. Otherwise the derived class destructors are not called because the there is compile time binding of the destructor pointer to the base class 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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Destructor in ActionScript Class seesamjagan Flash Actionscript Programming 2 09-30-2008 05:12 AM
What are Virtual Functions? How to implement virtual functions in "C"? Sabari C and C++ Programming 4 09-10-2007 11:35 PM
virtual objects simplesabita Testing Tools 2 08-28-2007 06:40 AM
Explain the need for "Virtual Destructor". Sabari C and C++ Programming 1 07-31-2007 12:18 AM
How can I create a virtual directory (or map virtual directory) for my website in TFS kingmaker ASP and ASP.NET Programming 1 07-27-2007 06:34 AM


All times are GMT -7. The time now is 03:36 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0