Creating a base component
With the theory covered, let's implement this into our project. The overwhelming message of this chapter has so far has been to use components to avoid messy inheritance, but we still need some inheritance as we need to use polymorphism!
Each object will be able to hold a range of components so we'll store them in a single generic
container. In order for us to do this we need to make use of polymorphism, ensuring that all components inherit from a common base class. That base class is what we're going to implement now.
Let's add a new class to the project and call it Component
. We'll leave it to you to implement the .cpp
:
#ifndef COMPONENT_H #define COMPONENT_H class Component { public: /** * Default Constructor. */ Component(); /** * Create a virtual function so the class is polymorphic. */ virtual void Update(float timeDelta) {}; }; #endif
Note that we've added a virtual update
function here as a class must have at least one virtual...