473,386 Members | 1,799 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,386 software developers and data experts.

Does destructor of derived class remove virtual table?

Hi list,

I have a small example which gives me "pure virtual function called".

I'm calling a virtual function in the destructor of the base class, where
the pointer used is effectively pointing to a derived class, but the whole
thing is in the process of being destroyed.

Interesting for me is also, that I get the same error if I call b->isA() in
the constructor of Base.

I just wanted to know, if this behavior is correct in C++ terms or is it a
compiler issue ? (using gcc-2.3.2 on Linux)

(Please CC me on mail)

#include <iostream>

class Base
{
public:
Base(Base *x) { b = x; };
virtual ~Base()
{
b->isA();
};

virtual int isA() = 0;

private:
Base *b;
};

class Derived : public Base
{
public:
Derived() : Base(this) { std::cerr << isA() << std::endl; };

virtual int isA() { return 1; }
};
main()
{
Derived *d = new Derived();

delete d;
}

Thanks,

Martin
Jul 22 '05 #1
3 2160
Martin Koller wrote:
Hi list,

I have a small example which gives me "pure virtual function called".

I'm calling a virtual function in the destructor of the base class,
where the pointer used is effectively pointing to a derived class, but
the whole thing is in the process of being destroyed.
In the destructor of the base class, the derived part of the objects is
already destroyed, so it isn't anymore an instance of the derived
class.
Interesting for me is also, that I get the same error if I call
b->isA() in the constructor of Base.
Same principle. In the base class constructor, the derived part of the
object does not yet exist. Therefore a polymorphic call won't call the
derived implementation of the virtual function.
I just wanted to know, if this behavior is correct in C++ terms or is
it a compiler issue ? (using gcc-2.3.2 on Linux)


It is correct.

Jul 22 '05 #2
On Sat, 21 Feb 2004 17:03:32 +0100 in comp.lang.c++, Martin Koller
<m.******@surfeu.at> was alleged to have written:
I have a small example which gives me "pure virtual function called".

I'm calling a virtual function in the destructor of the base class,


See also the topic "[23.3] When my base class's constructor calls a
virtual function on its this object, why doesn't my derived class's
override of that virtual function get invoked?" in Marshall Cline's C++
FAQ. The remarks regarding constructors mostly apply for the same
reasons to destructors. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
Jul 22 '05 #3
It is correct.


Thanks a lot for all replies.

Martin
Jul 22 '05 #4

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

Similar topics

11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
39
by: Ele | last post by:
Is it correct to say that Whenever a class has a virtual member function, define its destructor as "virtual"? Can a destructor as "pure virtual"? When is it needed to do so? For an interface,...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
8
by: lmfmaw | last post by:
Hi all, I've hit the wall with this "fairly" simple problem. As you can see in the code below, my destructors don't do their job as they are supposed to (I left them empty for this example). I'm...
6
by: Pallav singh | last post by:
Hi All How Does compiler implement virtual destructor ??? as we all know if base class destructor is virtual..........then while wriiting statemnt like this Base * b = new Derived( );...
3
by: GAURAV AGRAWAL | last post by:
Hi Guys, Can someone please explain me why this is happening #include<iostream> using namespace std; class a { public: int a1; // If I remove this it'll work fine
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.