What does the = 0 part of this declaration imply?
class KeyboardListener
{
public:
virtual bool keyPressed(void) = 0;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
Animalclass with a pure-virtual member functionfeed(): 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 thatfeed()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.)