Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 27th, 2006, 8:59 AM   #1
DogDays
Newbie
 
DogDays's Avatar
 
Join Date: Feb 2006
Location: Chicago
Posts: 1
Rep Power: 0 DogDays is on a distinguished road
Virtual Destructors

Every example I've found that demonstrates why the base class destructor needs to be virtual has the following basic code snippet.

DerivedClass* pDerived = new DerivedClass();
BaseClass* pBase = (BaseClass*)pDerived;
delete pBase;

Which for me raises the following questions.

1. Why deliberately miscast pBase to the wrong type? Sounds like a coding error to me.

2. If the consumer needs access to some functionality in the base class not explicitly provided by the derived class, then modify the OO design to provide it rather than "hack" your way into the base class.

I prototyped this with Visual C++ 6.0 and as long as you delete the same object you created, i.e. the derived class, the derived class destructor is always invoked without use of virtual.

I've been job searching and this seems to be a favorite quiz question.
Which brings up the concept of technical tests for interviews, but that's another story.
DogDays is offline   Reply With Quote
Old Feb 27th, 2006, 10:05 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
It's been a while since I've used C++, but I'll have a go at answering this:
Quote:
Originally Posted by DogDays
1. Why deliberately miscast pBase to the wrong type? Sounds like a coding error to me.
Presumably, the author is inferring from the names of the classes that DerivedClass inherits from BaseClass. Casting a class as one of its ancestors is legitimate.

Quote:
Originally Posted by DogDays
2. If the consumer needs access to some functionality in the base class not explicitly provided by the derived class, then modify the OO design to provide it rather than "hack" your way into the base class.
The author is using casting in this case to prove a point, rather than to be of any practical use. If a method is virtual, then an object will always use the "right" method, no matter what it is cast as. If a method is not virtual, then the method used will depend on what the object has been cast as.

If a destructor were not virtual, then it would change depending on what it was cast at. In the example you give, if the destructor were not virtual, C++ would call the destructor of BaseClass, rather than DerivedClass. This is a problem, because it could potentially lead to an object not being cleanly removed. For instance, if DerivedClass accessed a file, and BaseClass did not, then a non-virtual destructor could leave open filehandles lying around.

Because there is often little reason to not have virtual constructors, it's highly recommended for safety's sake. And if you don't have a virtual constructor, you essentially bar your class from being used in functions that take an ancestor class argument if there is a chance your object will be destroyed.
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC