Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

basic c++ polymorphism question

Posted on 2013-05-14
2
238 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

791 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