Solved

basic c++ polymorphism question

Posted on 2013-05-14
2
232 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
Comment Utility
>> 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
Comment Utility
Makes sense!  Thank you! :)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

8 Experts available now in Live!

Get 1:1 Help Now