By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

Does destructor of derived class remove virtual table?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
It is correct.


Thanks a lot for all replies.

Martin
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.