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. ...

Get Procedural Content Generation for C++ Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.