derivedAction<GameObject, Function> is a
function template, that is, when you fill in the template parameters, a
function is instantiated. There are two template parameters:
- GameObject: The derived class that you specify explicitly in calling code. A possible argument for this template parameter is Aircraft.
- Function: The type of callable object you pass as a function parameter, which is deduced by the compiler. "Callable object" can be a function pointer, a functor, or a lambda expression.
The whole idea behind
derivedAction() is to take a function that operates on derived classes, using the signature
void(GameObject& object, sf::Time dt)
and to transform it to a function that operates on base classes with the signature
void(SceneNode& object, sf::Time dt)
This allows to store multiple commands in the same container, although they internally dispatch to different classes.
These are rather advanced techniques, but they allow a great simplification. Without
derivedAction, we would have to downcast the parameter every time we define a command that runs on a derived class. The
derivedAction function template encapsulates this task.
If you don't understand some of the terminology I used in this post, I recommend looking up function templates, function objects and
std::function. These are very powerful C++ concepts, and the command system described in the book relies heavily on them.