Pure virtual destructor

D

David Côme

Hello everybody.
I have a question.
When i want use a pure virtual destructor in one of my classe, i must
give a definition of this destructor like this:

class A
{
//a lot of things
virtual ~A() =0;
};

A::~A()
{
//....
}

However,i ask myself why ?
Does anyone have one reponse ?
Thanks.

David Côme.
 
V

Victor Bazarov

David said:
Hello everybody.
I have a question.
When i want use a pure virtual destructor in one of my classe, i must
give a definition of this destructor like this:

class A
{
//a lot of things
virtual ~A() =0;
};

A::~A()
{
//....
}

However,i ask myself why ?
Does anyone have one reponse ?

If you ever derive from your class A, an instance of it will have
to be destructed at some point, most likely (unless all you do is
dynamically allocate the derived classes and never deallocate them
in your program). If your destructor is pure and not implemented,
then an attempt to destruct an instance of 'A' will result in
a call to a pure virtual function, which has undefined behaviour.
To avoid undefined behaviour you need to provide the implementation
for A::~A. That's one response I have.

V
 
D

David Côme

If you ever derive from your class A, an instance of it will have
to be destructed at some point, most likely (unless all you do is
dynamically allocate the derived classes and never deallocate them
in your program). If your destructor is pure and not implemented,
then an attempt to destruct an instance of 'A' will result in
a call to a pure virtual function, which has undefined behaviour.
To avoid undefined behaviour you need to provide the implementation
for A::~A. That's one response I have.

V


Thanks
 
J

johanatan

If you ever derive from your class A, an instance of it will have
to be destructed at some point, most likely (unless all you do is
dynamically allocate the derived classes and never deallocate them
in your program). If your destructor is pure and not implemented,
then an attempt to destruct an instance of 'A' will result in
a call to a pure virtual function, which has undefined behaviour.
To avoid undefined behaviour you need to provide the implementation
for A::~A.

Actually, looks like he already has the implementation defined. He
simply needs to remove '= 0' from the declaration. So, there's really
two problems: the first is trying to make the destructor pure virtual,
and the second is trying to define the destructor in the base class
after having made it pure virtual!!
 
D

Dave Rahardja

Actually, looks like he already has the implementation defined. He
simply needs to remove '= 0' from the declaration. So, there's really
two problems: the first is trying to make the destructor pure virtual,
and the second is trying to define the destructor in the base class
after having made it pure virtual!!

A pure virtual destructor serves a different need than the typical pure
virtual function. The syntax exists to allow you to define a virtual
destructor, and still have the class treated as abstract. This is
useful in certain cases where you want to define an abstract interface
or tag class through which a derived object can be deleted. Without the
=0 in the destructor, you'd have to define another pure virtual
function in order to make the base class abstract.

For instance,

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

class Foo: public Tag
{
public:
Foo();
virtual ~Foo();
};
Foo::Foo() {}

int main()
{
//Tag t; // <-- error, abstract.
Tag* pt = new Foo(); // <-- ok.
delete pt; // <-- ok, Foo::~Foo is called.
}

-dr
 
R

Rolf Magnus

johanatan said:
So, there's really two problems: the first is trying to make the
destructor pure virtual, and the second is trying to define the destructor
in the base class after having made it pure virtual!!

No, there is no problem. Both are valid. In fact, you _must_ implement a
pure virtual destructor.
 
J

johanatan

A pure virtual destructor serves a different need than the typical pure
virtual function. The syntax exists to allow you to define a virtual
destructor, and still have the class treated as abstract. This is
useful in certain cases where you want to define an abstract interface
or tag class through which a derived object can be deleted. Without the
=0 in the destructor, you'd have to define another pure virtual
function in order to make the base class abstract.

Ahh, Ok. Thanks for the clarification. This is definitely one of the
least intuitive parts of C++ I've ever encountered. Guess you learn
something new each day!
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top