Click to See Complete Forum and Search --> : HELP! delete function using base-class pointer


April 6th, 1999, 01:28 AM
Too tired to look this up right now...

Can a base-class ptr be used to delete an object of a derived class?

for instance (CMyWnd is derived from CWnd):

CMyWnd* pMyWnd = new CMyWnd; // create derived obj on heap
CWnd* pWnd = pMyWnd; // point base class ptr to obj of derived class
delete pWnd; // delete obj of derived class

this seems to work, but is it a clean deletion? For instance, if CMyWnd has additional data items (takes up more mem) will the CWnd* deletion remove the entire CMyWnd class structure?

Thanx for any help. I have a feeling that I've actually just read the answer to this, but right now everything is just falling out my ears.

Niall

Jerry Coffin
April 6th, 1999, 01:46 AM
Yes, it's legal as long as the base class contains a virtual destructor.


The universe is a figment of its own imagination.

April 6th, 1999, 02:07 AM
above code deletes the derived object by calling derivd class destructor since destrcctor os CWnd class is virtual member. derived class destructor should do cleening process