Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!brunix!doorknob!jak From: jak@cs.brown.edu (Jak Kirman) Newsgroups: comp.lang.c++ Subject: Re: Calling virtuals from destructors Message-ID: Date: 8 Jan 91 17:46:45 GMT References: <424@nec-gw.nec.com> Sender: news@brunix.UUCP Reply-To: jak@cs.brown.edu Distribution: na Organization: Department of Computer Science, Brown University Lines: 67 In-reply-to: stuart@NECAM.tdd.sj.nec.com's message of 8 Jan 91 06:03:21 GMT In article <424@nec-gw.nec.com> stuart@NECAM.tdd.sj.nec.com (Stuart Palmer) writes: - If you call a virtual member function from a destructor, what is - the behavior? We ran some tests and found differences between - Glockenspiel 1.2 and Sun 2.0 compilers. - - In Sun 2.0, if you call a virtual member function from - a destructor, the "actual" virtual function that is compiled into - the class vtbl will not get called. Rather, the virtual function - at the class level where the call was made gets called. If this is - confusing, see the code sample below. - - This is different than Glockenspiel 1.2. 1.2 calls the virtual - function from the vtbl (as might be expected?). - - Is this a bug in one of the compilers? I looked throught the Turbo - C++ bug list and found a similar bug concerning virtual functions - called from member functions. I would appreciate someone - describing the Correct (tm) behavior to me. - - Thanks! - - **********************************Code example******************************** - - class Base { - public: - > Base(); - > virtual ~Base(); - > virtual void some_func(); - } - Base::Base(){} - Base::~Base(){ some_func(); } - void Base::some_func(){ printf ("Base called.\n"); } - - class Derived : public Base { - public: - > Derived(); - > ~Derived(); - > void some_func(); - } - Derived::Derived(){} - Derived::~Derived(){ } - void Derived::some_func(){ printf ("Derived called.\n"); } - - main() { - > Derived d; - } From Ellis & Stroustrup ARM 12.7 Constructors and destructors Member functions may be called in constructors and destructors. This implies that virtual functions may be called (directly or indirectly). The function called will be the one defined in the constructor's (or destructor's) own class or its bases, but not any function overriding it in a derived class. This ensures that unconstructed objects will not be accessed during construction or destruction. The problem is that the some_func defined in Derived may access data belonging to the Derived object which is destroyed by the Derived destructor. Since the Derived destructor is called before the Base destructor, it would be unsafe to call functions which might operate on that data. The behaviour of Sun's compiler is correct. Jak jak@cs.brown.edu ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tell me what company thou keepest, and I'll tell thee what thou art. -- Cervantes