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 > Re: What's the different betteen pure virtual function and virtualfunction
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: What's the different betteen pure virtual function and virtualfunction

Réponse
 
LinkBack Outils de la discussion
Vieux 03/06/2008, 12h44   #1
Lars Uffmann
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the different betteen pure virtual function and virtualfunction

Hi Daniel,

Just so I get to learn something from this:

Daniel T. wrote:
> First, pure virtual:
>
> class Base1 {
> public:
> virtual void pure() = 0;
> };


So _pure_ refers to a virtual function definition and the = 0 will allow
for class definition without an actualy function body declaration? And
then you may not use the base class, and also

> class Derived1 { }; // will not compile

_must_ define the virtual function in the derived class that you want to
use?

And could you declare a pure virtual function, then derive another one
from that, then derive a third one and ONLY define the function body in
the third one, if that is the function you're going to use?
e.g.:
class Base { public: virtual void pure() = 0; };
class Derived : public Base { };
class TwiceDerived : public Derived { void pure() { cout << "twice
derived pure"; }

and then use
TwiceDerived td;
td.pure();
?

Best Regards,

Lars
  Réponse avec citation
Vieux 03/06/2008, 15h28   #2
Juha Nieminen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the different betteen pure virtual function and virtualfunction

Lars Uffmann wrote:
> So _pure_ refers to a virtual function definition and the = 0 will allow
> for class definition without an actualy function body declaration?


No. A pure virtual function is one which must be reimplemented in a
derived class. The "=0" is just syntactical notation to say that.
(Imagine it being a keyword like "pure" instead.)

The "=0" has nothing to do with whether you have to implement the
function or not. Any function can be declared but not implemented. (It's
just that if you try to call the function and it's not implemented,
you'll get a linker error.)
  Réponse avec citation
Vieux 04/06/2008, 10h41   #3
Lars Uffmann
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the different betteen pure virtual function and virtualfunction

Daniel T. wrote:
> That's part of it. Making the function pure (i.e., putting the "=0" at
> the end,) means you don't have to define the function for that class
> (you can if you want though.)
> [..more useful information..]


Thank you!
  Réponse avec citation
Vieux 04/06/2008, 10h43   #4
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: What's the different betteen pure virtual function and virtualfunction

On Jun 3, 4:28 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> Lars Uffmann wrote:
> > So _pure_ refers to a virtual function definition and the =
> > 0 will allow for class definition without an actualy
> > function body declaration?


> No. A pure virtual function is one which must be reimplemented
> in a derived class. The "=0" is just syntactical notation to
> say that. (Imagine it being a keyword like "pure" instead.)


You're right that the =0 is just syntactical notation (but it is
two separate tokens...you can insert white space between the two
characters, or even comments). But there's more to it than you
seem to be saying.

> The "=0" has nothing to do with whether you have to implement
> the function or not. Any function can be declared but not
> implemented. (It's just that if you try to call the function
> and it's not implemented, you'll get a linker error.)


Again, the rules are a bit more complicted. A function must be
implemented if it is "used". A function is used if it is
called, of course, or if its address is taken, but a virtual
function is also "used" anytime you create an instance of the
class, or of a class derived from it, unless it is pure. Making
a virtual function pure frees you from the obligation of having
to implement it (amongst other things).

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

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


É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,10292 seconds with 12 queries