473,322 Members | 1,562 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,322 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 2241
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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
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

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.