Reputation: 10403
What does the = 0
part of this declaration imply?
class KeyboardListener
{
public:
virtual bool keyPressed(void) = 0;
}
Upvotes: 3
Views: 284
Reputation: 6886
You have to implement this function in a derived class, it is purely virtual (as the name says) and has no implementation (which is implied by the zero, = 0) (there are cases where they can have implementations). Classes with pure virtual functions are called abstract base classes or a like. They can not be instantiated.
To make use of them you have to sublcass the abstract class and implement the pure virtual method(s) in the derived class.
Upvotes: 1
Reputation: 6668
The =0 denotes the function as pure virtual as opposed to plain virtual. The =0 defers defining a function body to a derived class.
The distinction makes the class which contains a pure virtual function an abstract one that cannot be instantiated, only extended into a proper class that can be, as long as the pure virtual function is given a body. This is typically utilised with pure virtual destructors since every derived class automatically provides a default destructor.
From https://stackoverflow.com/a/1219618/866333:
Note: The destructor is the only method that even if it is pure virtual has to have an implementation in order for the class it's defined in to be useful (yes pure virtual functions can have implementations).
Upvotes: 1
Reputation: 477348
This means the member function is pure virtual, which means it has no implementation at all. Consequently, the class cannot be instantiated (it becomes "abstract"), and it can only be used as a base class, whose derived classes must (eventually) implement the virtual member function.
An example would be an abstract Animal
class with a pure-virtual member function feed()
: Since every animal is always an instance of a concrete (i.e. derived) animal, no purely abstract animal can exist. And while very animal has some way of feeding, there is no universal base implementation that is common to every animal -- we only know that feed()
exists, but it must always be implemented concretely by a concrete derived animal.
(Note that you can actually provide an implementation for a pure-virtual function. You still cannot instantiate such a class, but derived classes can call the base function. This is very rarely useful and probably poor style.)
Upvotes: 6
Reputation: 12829
It is what is known as a "pure virtual function", and is how abstract classes are constructed. See the wikipedia article for more detail.
To use your example, in the KeyboardListener
class, the keyPressed(void)
function is declared but not implemented - that is, it is considered part of the interface, and child classes are expected to implement the function*, but the parent class itself does not. No instances of the parent class can be made - the parent class can only be used as a pointer to an instance of a fully-defined child class.
*That being said, child classes aren't required to implement it - but if they don't you can't instantiate those child classes either, only their descendants that finally implement keyPressed(void)
.
Upvotes: 1