Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!mips!nec-gw!stuart@NECAM.tdd.sj.nec.com From: stuart@NECAM.tdd.sj.nec.com (Stuart Palmer) Newsgroups: comp.lang.c++ Subject: Calling virtuals from destructors Message-ID: <424@nec-gw.nec.com> Date: 8 Jan 91 06:03:21 GMT Sender: news@nec-gw.nec.com Distribution: na Organization: NEC-AM TDD, San Jose, California Lines: 67 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; } **********************************Output************************************** 1.2 output: Derived called. 2.0 output: Base called. ****************************************************************************** -- Stuart Palmer stuart@tdd.sj.nec.com NEC America, Inc. 110 Rio Robles San Jose, CA 95134