Uzytkownik "TheFerryman" <fe***@onthenet.com> napisal w wiadomosci
news:25********************************@4ax.com...
Why doesn't compile? I would have thought the dtor of Base would call
Derived::f()
struct Base
{
virtual void f() = 0;
virtual ~Base(){f();}
};
struct Derived : public Base
{
virtual void f(){cout << "\nIn Derived f";}
};
int main()
{
Derived d;
return 0;
}
Destructor of Base cannot call Derived::f(), because Derived does not exist
at this point. At Base::~Base, d is of class Base, because Derived
destructor was already executed. In general, virtual function call mechanism
does not work during construction/destruction phase. Imagine what would
happen if Base::~Base actually called Derived::f() - a method of object
would be called after its destructor has already executed! It would break
important C++ 'contract', which states that no operation can be performed on
object before constructor or after destructor.
Best regards,
Marcin