Component functions
If we think about the behavior that we want, we need to be able to give an object a component of any given type. We also need to be able to fetch that same component later. We'll call these functions AttachComponent
and GetComponent
.
Earlier in the chapter, we identified how we can use templates to create a function with generic types and give them real values when we need them. We'll use templates and polymorphism to create these two functions.
Attaching a component
The first function that we're going to write will be used to attach a component of a given type to the Object
class. Since we've already identified that we're going to store all components in a single generic container, this function will be a relatively simple template. The only thing that we need to be aware of is that we should not add the same component twice!
Let's start by defining the container, as that's where we'll store the objects. Since we need to take advantage of polymorphism, we can't store actual...