PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Pure virtual destructor
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Pure virtual destructor

Réponse
 
LinkBack Outils de la discussion
Vieux 24/12/2007, 17h44   #1
David Côme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Pure virtual destructor

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.
  Réponse avec citation
Vieux 24/12/2007, 18h00   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pure virtual destructor

David Côme wrote:
> 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


  Réponse avec citation
Vieux 24/12/2007, 18h46   #3
David Côme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pure virtual destructor

On Mon, 24 Dec 2007 19:00:31 +0100, Victor Bazarov
<v.Abazarov@comacast.net> wrote:

> 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
  Réponse avec citation
Vieux 26/12/2007, 05h07   #4
johanatan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pure virtual destructor

On Dec 24, 1:00 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> David Côme wrote:
> > 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.


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!!
  Réponse avec citation
Vieux 26/12/2007, 05h35   #5
Dave Rahardja
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pure virtual destructor

On 2007-12-25 23:07:50 -0600, johanatan <johanatan@gmail.com> said:
>
> 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éponse avec citation
Vieux 26/12/2007, 09h44   #6
Rolf Magnus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pure virtual destructor

johanatan wrote:

> 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.

  Réponse avec citation
Vieux 27/12/2007, 00h54   #7
johanatan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pure virtual destructor

On Dec 26, 12:35 am, Dave Rahardja
<drahardja.place...@sign.here.pobox.com> wrote:
> On 2007-12-25 23:07:50 -0600, johanatan <johana...@gmail.com> said:
>
>
>
> > 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.
>


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!
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h03.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16371 seconds with 15 queries