22.10. Virtual methods

The keyword virtual in front of a parent class method tells the C++ compiler that the class has a child class which has a method which has the same name but which acts differently in the child class. If (a) a method is declared as virtual, and (b) the object which calls the method is referred to via a pointer, then (c) the compiled program will, even while it is running, be able to decide which implementation of the virtual method to use. It is worth stressing that this ‘runtime binding’ only works if you the programmer fulfill both conditions: (a) you use virtual in your method declaration, and (b) you use a pointer to your object.

Except in the case of a destructor, corresponding virtual functions have the same name. ...

Get Software Engineering and Computer Games now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.