Problem with virtual destructor

Z

ZikO

Hi

What's wrong with this code? Why can't I compile this?
I have created a virtual destructor in one of my code in a base class to
be sure objects of all derived classes would be destroyed. However, the
compiler says "no" with this comments:

C:\DOCUME~1\User\LOCALS~1\Temp/ccLAUiQR.o:test2.cpp:(.text$_ZN1BD2Ev[B::~B()]+0x17):
undefined reference to `A::~A()'
C:\DOCUME~1\User\LOCALS~1\Temp/ccLAUiQR.o:test2.cpp:(.text$_ZN1BD0Ev[B::~B()]+0x17):
undefined reference to `A::~A()'
C:\DOCUME~1\User\LOCALS~1\Temp/ccLAUiQR.o:test2.cpp:(.text$_ZN1BD1Ev[B::~B()]+0x17):
undefined reference to `A::~A()'
collect2: ld returned 1 exit status


This is a simple code which roughly represents the problem:

// code
#include <iostream>
using namespace std;
class A {
public:
virtual ~A() = 0;
};
class B : public A {};
class C : public B {};
class D : public C {};

int main(int argc, char *argv[])
{
D d;
A &a = d;
return 0;
}
// end of code

Without lines in main everything compiles successfully.

Best
 
Z

ZikO

Thanks Jeff. That's clear and should be obvious for me that without the
destructor the object can't be initiated. I can keep the destructor not
to be pure virtual. Just wanted to know why it's happened.

BTW what is the purpose of having pure virtual destructor

class A {
virtual ~A() = 0;
};

and then definition of it outside the class?

A::~A() {}

Regards.
 
Z

ZikO

Thanks Jeff. That's clear and should be obvious for me that without the
destructor the object can't be instantiated. I can keep the non-pure
virtual destructor. I simply wanted to know why it's happened.

BTW what is the purpose of having pure virtual destructor

class A {
virtual ~A() = 0;
};

and then definition of it outside the class?

A::~A() {}

Regards.
 
Z

ZikO

Jeff said:
For reasons that escape me, C++ syntax doesn't allow pure virtual
function definitions in the class definition.

Right. It makes sens. thanks =)

Regards.
 
Z

ZikO

Jeff said:
> For reasons that escape me, C++ syntax doesn't allow pure virtual
function definitions in the class definition.

Right.
It looks like the tricks to freak the compiler out. You are defining an
abstract class with a pure virtual destructor but simultaneously
"giving" it your own definition of such destructor to avoid the compiler
to do it for you.

It makes sens. thanks =)
 
K

Kaz Kylheku

Hi

What's wrong with this code? Why can't I compile this?
I have created a virtual destructor in one of my code in a base class to

You have? Prove it. Where is the body of this function you have created?
#include <iostream>
using namespace std;
class A {
public:
virtual ~A() = 0;
};
class B : public A {};
class C : public B {};
class D : public C {};

I can't see it.
 
Z

ZikO

Kaz said:
class to
>
> You have? Prove it. Where is the body of this function you have created?

You cannot find the body of this function.
Do I really have to explain why? Isn't it really obvious?
>
> I can't see it.

I really do not know the point of your comments. Here we are after
almost 2 hours when everything was explained. How about reading other
posts and trying to add something really useful if there is anything else?

Regards
 
R

Rolf Magnus

ZikO said:
Thanks Jeff. That's clear and should be obvious for me that without the
destructor the object can't be instantiated. I can keep the non-pure
virtual destructor. I simply wanted to know why it's happened.

BTW what is the purpose of having pure virtual destructor

class A {
virtual ~A() = 0;
};

and then definition of it outside the class?

A::~A() {}

Making the destructor pure virtual instead of just virtual has the same
effects as it has for normal member functions:

- you force derived classes to explicitly define a destructor
- your base class becomes abstract
 
I

Ike Naar

Making the destructor pure virtual instead of just virtual has the same
effects as it has for normal member functions:

- you force derived classes to explicitly define a destructor

No. See below.
- your base class becomes abstract

True.


The derived class need not explicitly define a destructor.
If it doesn't, the compiler automatically creates a default
destructor for it.

You must however (as has been mentioned earlier in this thread)
provide a definition for the base class constructor, even if it's
declared pure virtual.

class Base {
public:
virtual ~Base() = 0;
};

Base::~Base() {} /* although pure virtual, definition must be provided */

class Derived : public Base {
/* no explicit destructor needed */
};

int main()
{
Derived x; /* instantiate a Derived object, no problem */
return 0;
}

If you omit the definition for ~Base, there will be a problem at
link time, no matter if you define ~Derived explicitly or not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top