Solved

basic c++ polymorphism question

Posted on 2013-05-14
2
235 Views
Last Modified: 2013-05-14
Hi Experts,

I have a tree of different kinds of objects.  i.e., the child nodes of higher level parents are of different types.  However, they all implement a similar interface - ISomething.

If I delete any of the objects by using the interface pointer like this:

delete (ISomething*) theObject;

Am I also safely deleting the underlying full object?  I think not.

Thanks for confirming,
Mike
0
Comment
Question by:thready
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39165249
>> Am I also safely deleting the underlying full object?

As long as the base class destructor is virtual, you are - see http://www.codersource.net/c/c-miscellaneous/c-virtual-destructors.aspx ("C++ Virtual destructors") and http://blogs.msdn.com/b/oldnewthing/archive/2004/05/07/127826.aspx ("When should your destructor be virtual?"). It's actually a simple thing to do, and may have heavy impact if you don't use it.
0
 
LVL 1

Author Closing Comment

by:thready
ID: 39165264
Makes sense!  Thank you! :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now