Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!ucsd!rutgers!att!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: Delete-ing class hierarchie on free store Keywords: delete, inheritance, free store, base pointer Message-ID: <9230@alice.UUCP> Date: 23 Apr 89 11:06:42 GMT References: <518@atcmpe.atcmp.nl> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 20 In article <518@atcmpe.atcmp.nl>, leo@atcmp.nl (Leo Willems) writes: > If there are two classes, X and Y, Y derived from X, (both with > destructors) the destructor Y::~Y() is -not- called in the next situation: > X *x = new Y; // ok: base pointer assignment > delete x; // only X::~X() called > Only the destructor for X is called. The situation shown above is invalid because x points at a Y, not an X. To get this to work properly, either you must write delete (Y*) x; or X must have a virtual destructor. The virtual destructor route is recommended if you have any control over the definition of X. -- --Andrew Koenig ark@europa.att.com