Well, it looks like I am learning some things at school.
In my Object Oriented Design class, we were discussing the usefulness of the
singleton class. I've been doing just this before, but in a less than elegant manner.
First, a singleton is an object that can only be instantiated
once. It handles this by making its construtor(s) private, and having a class method called "instance", which returns a pointer to an instantiated object. This pointer being a class attribute of the said class.
Now, the elegance of this method is that it doesn't pollute the global namespace (in C++). I've always done something along these lines:
static Whatever * instance = NULL;
Whatever * Instance(void)
{
if( instance == NULL )
instance = new Whatever();
return instance;
}
Now, in my implementation, there's a big ugly function "::Instance()" in the global namespace, which is fine for small applications, but lousy for larger ones, or libraries.
The singleton fixes this by having its initializer scoped by the class name.
Nice.
=====
Some handy links: