Article View: comp.lang.c++
Article #2649Virtual destructors
From: pj@hrc63.co.uk (
Date: Mon, 27 Feb 1989 16:24
Date: Mon, 27 Feb 1989 16:24
51 lines
1312 bytes
1312 bytes
Stroustrup does not seem to deal with the question of destructing derived classes when all the calling function has is a pointer to the base class. By default only the base class destructor gets called. In Oregon C++ (for Suns) v 1.1, it is possible to declare the base destructor as virtual. This then ensures that the appropriate destructor is called for any derived classes provided that the _derived_class_has_a_destructor_. If the derived class has no explicit destructor then any member destructors will not be called (rather than being called implicitly as they would be normally). Does anyone know how other compilers behave? e.g. class Base { int foo; public: Base( ); virtual ~Base( ); // This is legal. }; class Derived_1: Base { Problem mung; // Destructor should be called implicitly public: Derived_1( ); // Constructor but no destructor }; class Derived_2: Base { Problem mung; // Destructor should be called implicitly public: Derived_2( ); // Constructor ~Derived_2( ); // and Destructor }; main( ) { Base *p1, *p2; p1 = new Derived_1; p2 = new Derived_2; // p1 & p2 both point to base classes of Derived_1/2. delete p1; // Will not call Problem::~Problem. delete p2; // Will call Problem::~Problem. }; // Paul Johnson.
Message-ID:
<551@hrc63.co.uk>
Path:
rocksolid-us.pugleaf.net!archive.newsdeef.eu!mbox2nntp!utzoo!attcan!uunet!mcvax!ukc!stl!stc!root44!hrc63!pj