473,327 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

confusion with abstract methods.

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;
}
Jul 22 '05 #1
6 2242
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


Jul 22 '05 #2
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


Jul 22 '05 #3
On Mon, 5 Apr 2004 13:58:24 +0200, "Marcin Kalicinski"
<ka****@poczta.onet.pl> wrote:
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


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

Jul 22 '05 #4
On Mon, 5 Apr 2004 13:58:24 +0200, "Marcin Kalicinski"
<ka****@poczta.onet.pl> wrote:
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


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

Jul 22 '05 #5
Marcin Kalicinski wrote:
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".

Jul 22 '05 #6
Marcin Kalicinski wrote:
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".

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove...
0
by: Benjamin | last post by:
Hi, I am unable to access all properties and methods in a COM .DLL that I have. The properties and methods exposed by the .dll all appear as "abstract" when I view them in the Object Browser. ...
10
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract...
18
by: Bradley | last post by:
I'm trying to determine if there's a general rule for when an Interface should used vs. an Abstract Class. Is there any design advantage to using one or the other? Brad
5
by: Tony Johansson | last post by:
Hello!! Assume you have an Interface called ITest with these three method declarations. interface ITest { void foo1(); void foo2(); void foo3(); }
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
4
by: Gert Kok | last post by:
The microsoft page http://msdn2.microsoft.com/en-us/library/9fkccyh4.aspx states: Remarks (nr 4) Virtual properties behave like abstract methods, except for the differences in declaration...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
4
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
5
by: Tony Johansson | last post by:
Hello! Here I have an Interface called ITest and a class called MyClass which derive this intrface. As you can see I don't implement this method myTest in class MyClass because i use the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.