Connecting Tech Pros Worldwide Forums | Help | Site Map

Pure virtual destructor

=?utf-8?Q?David_C=C3=B4me?=
Guest
 
Posts: n/a
#1: Dec 24 '07
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.

Victor Bazarov
Guest
 
Posts: n/a
#2: Dec 24 '07

re: Pure virtual destructor


David Côme wrote:
Quote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


=?utf-8?Q?David_C=C3=B4me?=
Guest
 
Posts: n/a
#3: Dec 24 '07

re: Pure virtual destructor


On Mon, 24 Dec 2007 19:00:31 +0100, Victor Bazarov
<v.Abazarov@comacast.netwrote:
Quote:
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
johanatan
Guest
 
Posts: n/a
#4: Dec 26 '07

re: Pure virtual destructor


On Dec 24, 1:00 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Quote:
David Côme wrote:
Quote:
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:
>
Quote:
class A
{
//a lot of things
virtual ~A() =0;
};
>
Quote:
A::~A()
{
//....
}
>
Quote:
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.
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!!
Rolf Magnus
Guest
 
Posts: n/a
#5: Dec 26 '07

re: Pure virtual destructor


johanatan wrote:
Quote:
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.

Closed Thread