Operator overloading in C++
We now have a good foundation to build on, but we can actually make our object pool much nicer to use. One of the cooler features in C++ is the fact that you can override the default behaviors of operators, typically referred to as operator overloading. This is done with functions being created with specific names that contain the operator keyword, followed by what operator you want to define. Just like regular functions, they have return types as well as parameters that get passed to them.
Note
For more information on operator overloading and how it works in C++, check out http://www.cprogramming.com/tutorial/operator_overloading.html.
In addition to common operators, such as +
, -
, and /
, we also have the ability to overload the new
and delete
operators as well, allowing us to use our own custom object pool instead!
To do this, we will need to add the following to the end of the GameObject
class, and add the following bold lines to the class definition:
class GameObject...