You usually mark class as virtual
when, besides having some sort of implementation (either directly in the class declaring the function itself or in a child inheriting this class), you want the declaration to state a contract, which must be fulfilled by the implementations.
The declaration basically says: If you give me this set of parameters of these types, I am guaranteed to return this type of a variable (the return
type). You do not need to care what happens inside the function itself, but you can be pretty sure these if you give me some parameters of these types, you can be pretty sure I'll give you the return type back.
This is called abstraction, when the pivots of your application are the declarations providing expected but not exact behaviours. Interfaces, or in C++ classes containing nothing but pure virtual methods, are exactly that. They define a contract but don't provide the implementation.
If you want to unit test, what you want in your code is to depend on nothing but virtual
functions. You can do that by either defining a function of a class already providing behaviour as virtual
or extracting the function's declaration to a whole new class representing the interface of the function (making the declaration pure virtual), have the initial class inherit from this newly created abstract class and then use this newly created abstract class throughout your project.
#define VITRUAL
, whcih expands to eithevirtual
or nothing, deponing on#ifdef TESTING