implement (virtual) destructor

Asked by Edward Benson on 2011-07-16

Dear Advanced c/g++ programers:

I tested a simple program about Creating an interface with an Abstract Base Class. from book (C++ cookbook) page 308, 309. Example 8-11. using a pure interface.
// Example 8-11 Using a pure interface
class Person {
virtual void eat() = 0;
virtual void sleep() = 0;
virtual void walk() = 0;
virtual void jump() = 0;

class IAirbone {
virtual void fly() = 0;
virtual void up() = 0;
virtual void down() = 0;

class Superhero : public Person, // A superhero *is* a person
public IAirbone { // and flies
virtual void eat();
virtual void sleep();
virtual void walk();
virtual void jump();
virtual void fly();
virtual void up();
virtual void down();
virtual ~Superhero();

void Superhero::walk() {
// ...

void Superhero::fly() {
// ...

// Implement all of the pure virtuals in Superhero's Superclasses...

int main() {

Superhero superman;
superman.walk(); // Superman can walk like a person; // or fly like a bird
my g++ 4.5.2 (on linux2.6.35-25) response by
eric@eric-laptop:~/cppcookbook/ch8$ g++ Example8-11.cpp
/tmp/ccT3nO5t.o: In function `main':
Example8-11.cpp:(.text+0x47): undefined reference to `Superhero::~Superhero()'
/tmp/ccT3nO5t.o: In function `Superhero::Superhero()':
Example8-11.cpp:(.text._ZN9SuperheroC2Ev[_ZN9SuperheroC5Ev]+0x24): undefined reference to `vtable for Superhero'
Example8-11.cpp:(.text._ZN9SuperheroC2Ev[_ZN9SuperheroC5Ev]+0x2e): undefined reference to `vtable for Superhero'
collect2: ld returned 1 exit status
actually that book even did not specially define Superhero::walk(), that is
I add by myself to escape my compile's error(is that right? or book's is right?)
you can download the source code of that book's example and test by yourself
according to book, thses code are compile good in visual c++ 7.1 on window xp
thanks your help a lot in advance, Eric
jsmith (5668) Jul 15, 2011 at 5:07pm
The linker error is because you didn't implement the destructor for Superhero (or at least that's what it's saying).
fsshl (4) Jul 15, 2011 at 6:33pm
so, do you know how to implement (the destructor for Superhero)?(easyest)
thanks your code a lot in advance(I am waiting)

Question information

English Edit question
Ubuntu gcc-defaults Edit question
No assignee Edit question
Last query:
Last reply:

Assuming there are no specific actions that need to be undertaken when a Superhero object is destructed, and you are only implementing the virtual destructor so that a Superhero* pointing to an object whose type inherits Superhero will, upon deletion, have the correct derived destructor invoked, you can just implement a trivial destructor (i.e., one that does nothing when called):

virtual ~Superhero() { }

You can put that in the class body itself, in place of the similar-looking line. Doing so makes it much clearer to see what's going on, and there are no disadvantages to the destructor's implementation being inline. (On the other hand, if you're writing a library and you want the same header files to be usable with a future version in which Superhero has a nontrivial destructor, you could define Superhero::~Superhero outside the class body, in the source file.)

By the way, you should be aware that, with the code you have written, if Foo is a derived class of Superhero with a nontrivial destructor and p is a Person* or IAirborne* pointing to a Foo, deleting p will not invoke Foo::~Foo. (If all your polymorphic pointers to objects that are Superheros are of type Superhero* or more derived pointer types, this is not a problem.)

Can you help with this problem?

Provide an answer of your own, or ask Edward Benson for more information if necessary.

To post a message you must log in.