|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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.) |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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! ![]() |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|