What’s a virtual function?

Print anything with Printful



Virtual functions are functions defined in a superclass that must exist in a subclass for a complete class definition. They can be defined as a stub or pure virtual function. Virtual functions can reduce code duplication, but can have pitfalls with multiple inheritance.

A virtual function is a function, defined in a superclass, that must exist in a subclass for that subclass to have a complete class definition. Virtual functions are based on an object-oriented programming paradigm called virtual inheritance, which is most commonly seen in C++ using the “virtual” keyword. To define a virtual function you need two classes, a superclass and a subclass. The superclass is where the function is first declared and possibly defined. The subclass is where the function is defined or overridden, depending on whether the function was defined in the superclass.

The virtual function can be defined in two ways. First, it can be defined as a stub, where it has an empty body and does nothing. Second, it could be defined as a pure virtual function, where it is defined as NULL in the superclass header file.

There are advantages and disadvantages to both methodologies. Defining a function as a stub ensures that all subclasses have some implementation of it, even if it does nothing. If you forget to override the function and implement it correctly in a subclass, however, you won’t see any errors or warnings to let you know. Defining a pure virtual function, on the other hand, requires that each subclass has its own definition of the function, and if it doesn’t you will get errors.

Virtual functions, however, are subject to the same inheritance rules as non-virtual functions, so inheritance hierarchies with more than two levels may not require explicit definitions of virtual functions. For example, consider class A declaring a virtual function, implemented in subclass B. Class B has its own subclass, class C. Class C does not require an explicit function definition of class A, because it inherits the definition from class B. If necessary, class C can override class B’s function, or it can override class B’s function while calling it.

At the other extreme, virtual functions need not be defined in a subclass if they are declared virtual in that subclass. For example, one can consider a class A that declares a virtual function and has two subclasses, B and C. Also, one could imagine that class B has subclasses D and E, and subclass C has subclasses F and G.

All classes B to G must have the virtual function of class A defined in some way. If class B has an implementation of A’s function, classes D and E don’t need to do it again. Maybe the subclasses of C need to implement the function of A, but both do something different, so defining the function in the same class C wouldn’t be useful. In that case, the function can be declared virtual in class C and an implementation is not needed.
Virtual functions can be daunting to learn, but when used correctly, they can reduce code duplication and make code much easier to understand overall. There are many pitfalls with virtual functions, however, especially with regards to multiple inheritance. In multiple inheritance, it is possible for ambiguously defined virtual functions to conflict with each other, so they should be used with caution in that context.




Protect your devices with Threat Protection by NordVPN


Skip to content