April 2020
Intermediate to advanced
412 pages
9h 58m
English
In this application, we use the same idea (static arrays of preallocated objects) that we used in the first recipe; however, we wrap it into a templated ObjectPool class to provide a generic interface for handling objects of different types.
Our template has two parameters—a class or a data type of objects stored in an instance of the ObjectPool class, and the pool size. These parameters are used to define two private data fields of the class—an array of objects and an array of free indices:
T objects[N]; size_t available[N];
Since template parameters are being resolved at compile time, these arrays are allocated statically. Additionally, the class has a private data member called top that acts as an index in the available ...
Read now
Unlock full access