Oliver Tappe wrote:
Hi, On 2008-03-07 at 22:30:45 [+0100], Rene Gollent <anevilyak@xxxxxxxxx> wrote:On Fri, Mar 7, 2008 at 3:24 PM, Stephan Assmus <superstippi@xxxxxx> wrote:What happens if I call a virtual function from within the destructor of a base class? Will I always get the base class version of the function?Nope, you'd have to explicitly say baseclassname::function() if you want the base version to my awareness.I have not checked with the standard, but I'd assume that from a destructor you can only reach methods from that class "upwards", as all the subclasses are already destroyed (their destructors were already executed). So I'd think that calling a method from a base class' destructor should invoke the implementation in that base class, not in any subclass of it. Just as it in the constructor, I suppose.
You're right. I checked with my C++ book and, to be sure, with a bit of code:
#include <iostream> using std::cout; class A { public: A(void) { cout << "A constructor\n"; }; ~A() { cout << "A destructor\n"; Method(); }; virtual void Method(void) { cout << "A method\n"; }; }; class B : public A { public: B(void) { cout << "B constructor\n"; }; ~B() { cout << "B destructor\n"; Method(); }; virtual void Method(void) { cout << "B method\n"; }; }; int main (int argc, char * const argv[]) { B* b = new B(); delete b; return 0; } As expected, the output is A constructor B constructor B destructor B method A destructor A method Regards, Gabriele