confusion with abstract methods.

T

TheFerryman

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;
}
 
M

Marcin Kalicinski

Uzytkownik "TheFerryman said:
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
 
T

TheFerryman

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

Ah yes! I see that now. Thanks for your explanation.
 
R

Rolf Magnus

Marcin said:
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.

Actually, it does work. You just have to remember that the object isn't
of the Derived class anymore in the Base destructor. There is a
difference. Think of the following example:

#include <iostream>

class Base
{
public:
virtual void foo() { std::cout << "Base\n"; }
void x () { foo(); }
};

class Derived : public Base
{
public:
void foo() { std::cout << "Derived\n"; }
~Derived() { x(); }
};

class Another : public Derived
{
public:
void foo() { std::cout << "Another\n"; }
};

int main()
{
Another a;
}

The output of this program would be "Base" if polymorphism wouldn't work
in the destructor, but it does print "Derived".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top