Path: utzoo!yunexus!geac!syntron!jtsv16!uunet!husc6!rutgers!att!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: destructors, derived classes, and pointers Message-ID: <8372@alice.UUCP> Date: 30 Oct 88 17:31:55 GMT Article-I.D.: alice.8372 References: <16219@agate.BERKELEY.EDU> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 47 In article <16219@agate.BERKELEY.EDU>, pete@violet.berkeley.edu (Pete Goodeve) writes: > The problem is that all the managing procedure has is a pointer to the > object (of the base type naturally). Now I would expect that a delete call > on this pointer would detect the correct destructor to invoke before it > actually disposed of the memory, but NOOO! It calls ONLY the parent type > destructor!!! So much for nice neat cleanup... The solution to your problem is to declare the destructor as virtual in the base class. For more detail, see my article ``An example of dynamic binding in C++'' in the Journal of Object-Oriented Programming, vol. 1, #3. Example of the syntax: class Node: { // stuff public: Node(); virtual ~Node(); // more stuff }; class SpecialNode: public Node { // stuff public: SpecialNode(); ~SpecialNode(); // more stuff }; You only need to make the destructor virtual in the base class. You don't -- and indeed, can't -- make any constructors virtual. Caveat: some versions of the C++ translator have a bug that doesn't get things quite right unless every derived class has an explicit destructor. To avoid getting bitten by this bug, write a destructor even if you don't need it: class NodeWithEmptyDestructor: public Node { // stuff public: ~NodeWithEmptyDestructor() { } }; -- --Andrew Koenig ark@europa.att.com