Re: What's the different betteen pure virtual function and virtual function
Lars Uffmann <aral@nurfuerspam.de> wrote:
Daniel T. wrote:
Just so I get to learn something from this:
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?
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.)
And then you may not use the base class, and also
You can use the base class, but you cannot instantiate an object
directly from the base class.
_must_ define the virtual function in the derived class that you want to
use?
More properly, some class in the hierarchy must define that member
function for objects of its type or you won't be able to directly
instantiate objects of that type.
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();
?
Yes.