-
Notifications
You must be signed in to change notification settings - Fork 5
Using Derived Types with Utility
Leeor Dicker edited this page Mar 8, 2017
·
1 revision
Most often when using the Utility<T>
class you're using the exact type you need. However in some cases you will need to use a derived type. Two notable examples in NAS2D itself are the Renderer and Mixer classes which specify an interface but leave the implementation up to more derived types.
In these cases you will need to make use of Utility<T>::instantiateDerived(T* t)
as follows:
// Create a Renderer utility with a specific implementation
Utility<Renderer>::instantiateDerived(new OGL_Renderer());
Renderer& r = Utility<Renderer>::get(); // gets a reference to the derived Renderer type OGL_Renderer
// 'r' can now be used without needing to specify the implementation
r.drawBox(10, 10, 25, 25, 255, 0, 255, 255);
r.drawPoint(100, 100, 0, 255, 0, 255);
// etc.