Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Does destructor of derived class remove virtual table?

Reply
Thread Tools

Does destructor of derived class remove virtual table?

 
 
Martin Koller
Guest
Posts: n/a
 
      02-21-2004
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
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      02-21-2004
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.

 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      02-21-2004
On Sat, 21 Feb 2004 17:03:32 +0100 in comp.lang.c++, Martin Koller
<(E-Mail Removed)> 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/


 
Reply With Quote
 
Martin Koller
Guest
Posts: n/a
 
      02-22-2004

> It is correct.


Thanks a lot for all replies.

Martin
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Runtime Error for virtual derived class destructor pritesh kadam C++ 1 07-13-2012 12:50 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table? sojin C++ 12 04-07-2006 09:02 AM
Virtual destructor for virtual base class? Chunhui Han C++ 2 06-24-2004 10:13 AM



Advertisments