Just a short C++ code design question this time, namely how and where to generate default class instances.
Say I have a class that requires another class as a parameter storage, for example a class CWindow, which needs a CGuiSkin class to decide how to draw, what click regions to use and similiar.
The CGuiSkin class needs to be shared for several windows, and thus cannot be included in the CWindow class itself as a class member.
So, I added a CGuiSkin pointer to CWindow. When this pointer is Null, I want to use some default parameter set (so the user of my class need not fill a parameter storage if he doesn't want to). In turn, I can then write a method CGuiSkin& CWindow::GetWindowSkin() which always returns a valid parameter storage (either the pointer target, or the default one).
Now, the question is where and how to implement this default parameter object.
The first idea was to make it a static class member of the CGuiSkin class, which works well for simple objects. However, it doesn't work as well for more difficult classes (consider dynamic memory allocation in the constructor: the class instance is created even before my main() method, so there is basically no way to implement proper error handling)
The next idea was to implement the default instance as a singleton object, and create it the first time a CWindow has a Null pointer and still wants to retrieve a CGuiSkin object. The disadvantage here is object deconstruction, basically you'd just keep the object around until the OS (maybe) cleans it up after program shutdown, which also seems a bit hackish.
Last idea I had was to add a instance pointer to the CGuiSkin class similiar to the one used for a singleton implementation, but manually create / destroy the appended object at some place in program initialization and shutdown. The problem here is it's not automated, and consequently hard to maintain.
I like the first method presented best, since it comes with minimal additional coding work.
Is there some design pattern for handling such default instances ?
Anybody out there got any better idea for the implementation of the described scheme ?
Thank you for your time,