3

What does the = 0 part of this declaration imply?

class KeyboardListener
{
  public:
    virtual bool keyPressed(void) = 0; 
} 
Roman
  • 10,309
  • 17
  • 66
  • 101

4 Answers4

6

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.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • If I had to guess, I'd say that the `= 0` reflects the fact that the function pointer in the class's vtable is null, though I have no idea if that's actually how pure-virtual functions are implemented... – Kerrek SB Dec 13 '11 at 01:45
  • 1
    I think your note text needs to be bigger regarding the statement that there CAN be an implementation for a pure virtual method - many other answers here are stating that pure virtual means "no implementation", which is incorrect. – franji1 Dec 13 '11 at 01:45
  • @KerrekSB, does this mean as long as there is at least one purely virtual method in the class, it can no longer be instantiate? – Roman Dec 13 '11 at 01:47
  • @Am.: Correct. This is also something you should be able to try for yourself! :-) The abstractness is also inherited by derived classes which do not provide overrides. You only get a concrete class once all ancestral pure-virtuals have been implemented at some point. – Kerrek SB Dec 13 '11 at 01:48
  • @KerrekSB, you're answers are really helping me get through C++ fast. I'm trying to learn it on steroids for a job I've picked up. I'm mainly a Javascript and C# developer. By the way do you get any sleep? You've pretty much answered everyone of my C++ questions. – Roman Dec 13 '11 at 01:50
  • @Am: I'm glad :-) Yeah, I typically doze off while answering PHP questions :-) – Kerrek SB Dec 13 '11 at 01:52
  • @KerrekSB. Thanks for that last bit about deriving classes. That's really subtle. – Roman Dec 13 '11 at 01:52
  • @Am.: Well, it's sort of the only way that makes sense... in any case, do go and write a few example classes yourself, and you'll get the hang of this in no time. Modern compilers tend to give very helpful diagnostics for these sort of things. – Kerrek SB Dec 13 '11 at 01:54
  • @KerrekSB, let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5792/discussion-between-am-and-kerrek-sb), I'd like to ask you something about debugging c++ code. – Roman Dec 13 '11 at 02:00
  • @KerrekSB, Like most code, it's useful where needed, e.g. quite useful in a pure virtual destructor that needs to free up resources allocated in the base class. Quite useful as a default implementation for a pure virtual, but where you want derived class(es) to explicitly implement a call to the base class implementation in its override of the method. The main point is that "pure virtual" does NOT mean "no implementation". – franji1 Dec 13 '11 at 02:28
1

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).

Nate
  • 12,499
  • 5
  • 45
  • 60
1

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.

drahnr
  • 6,782
  • 5
  • 48
  • 75
1

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).

Community
  • 1
  • 1
John
  • 6,433
  • 7
  • 47
  • 82