Sign in to follow this  
the_moo

derived classes and new-delete

Recommended Posts

hey all, just a quicky this one: i have a base class "BaseObject" which i have derived others from - "MeshInstance" and "SpriteInstance". if i have two pointers to the same object:
BaseObject *ptrbase;             //imagine pointer is pointing to something (i.e. not NULL)
MeshInstance *ptrmesh = ptrbase; //now both point to the same thing
what happens if i do:
delete ptrbase;
?? i was just wondering if this removes all memory including that which is occupied by members in MeshInstance since it is using the BaseObject pointer rather than if i had done delete ptrmesh; ... i just thought about it and wasn't sure (plus couldn't find the answer anywhere). thanks for any info,

Share this post


Link to post
Share on other sites
Quote:
Original post by Endurion
It will, but only if you have a virtual destructor.


Assuming that the code is something like:


#include <iostream>

class BaseObject
{
int mTemp;
public:
BaseObject() : mTemp(1) {}
~BaseObject()
{ std::cout << mTemp << std::endl; }

virtual void justForPolymorphism(void){ }
};

class DerivedObject: public BaseObject
{
int mTemp;
public:
DerivedObject() : BaseObject(), mTemp(2) {}
~DerivedObject()
{ std::cout << mTemp << std::endl; }
};

int main(void)
{
BaseObject* baseObjectPtr = new BaseObject();
DerivedObject* derivedObjectPtr = dynamic_cast<DerivedObject*>(baseObjectPtr );
if(derivedObjectPtr == 0){ std::cout << "CAN'T DYNAMICALLY CAST THE BaseObject POINTER!" << std::endl; }
delete baseObjectPtr; // OK, Prints 1
system("PAUSE");
}





It will not, in fact, need a virtual destructor in BaseObject to delete the baseObject data. This is because baseObjectPtr is being deleted, not derivedObjectPtr.

However, if the above main function was:


int main(void)
{
DerivedObject* derivedObjectPtr = new DerivedObject();
BaseObject * baseObjectPtr = derivedObjectPtr;
delete baseObjectPtr; // Prints 1 BUT DOES NOT PRINT 2! Did not delete the DerivedObject data!
system("PAUSE");
}




You would definately need a virtual base destructor in order to delete the derived objects data (and you should always have one, if a class will ever be inherited from).

In reply to the original question then:
1. You cannot just assign a base class pointer to a derived class pointer, you must use dynamic_cast.
1.1 You can't dynamically cast a base object pointer to a derived object pointer, if the data pointed to by the base pointer is not derived object data.

2. Deleting the ptrbase is fine. If ptrbase in fact points to MeshInstance data, then BaseObject MUST have a virtual destructor to destroy the MeshInstance specific data.

3. If assigning ptrbase to ptrmesh (through dynamic_cast) succeeded, then deleting ptrbase will result in ptrmesh pointing to bad data. If you then assigned something else to ptrbase, ptrmesh would still point to bad data.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this