Programming Forums
User Name Password Register
 

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

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
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
 

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 10:44 PM.

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