do, in theory, with global variables but ICK!
That is interesting, although I'm having trouble imagining a situation
I'd use it in.
The problem with singleton is that people abuse it, as can be seen by
the fact that very few participating in this discussion seem to
understand its intended purpose: enforcing a creational policy.
My use of it seems a little bit unusual... maybe there is a better
way. Basically I have something like this (although I've greatly
simplified):
=== begin example ===
// Some base class.
class ThingBase {
...
};
// Some default implementation of ThingBase.
class DefaultThing : public ThingBase {
public:
static DefaultThing * getInstance ();
private:
DefaultThing (...);
};
// And many other ThingBase derived classes exist.
// SomeClass uses a ThingBase:
class SomeClass {
public:
// This takes a ThingBase, but NULL can be specified to
// use the default implementation.
SomeClass (ThingBase *thing, ...)
: thing_(thing ? thing : DefaultThing::getInstance())
{
}
private:
ThingBase *thing_;
};
=== end example ===
Like I said, I simplified -- there are other ways to construct a
SomeClass with a default "thing" besides just passing NULL to a
constructor.
What I am trying to do here is avoid having to instantiate a new
DefaultThing in SomeClass if the default was to be used, because then
I would have to keep track of whether or not I actually did that, and
add some logic to delete "thing_" in ~SomeClass() if so. I do this
same thing in other places besides SomeClass, and using a single
instance of DefaultThing simplified things all around. The
implementation details of DefaultThing do make it safe to use a single
instance from everywhere, by the way.
I had also considered simply having a static DefaultThing instance in
SomeClass (and other static instances in other classes besides
SomeClass). It's not a problem to instantiate more than one
DefaultThing, the only requirement is a simple way to eliminate clean
up logic.
But... I was confused (and still am a little) about static
initialization order and wasn't sure if that was a good idea or not.
I'm trying to impose as little usage requirements as possible because
eventually other developers are going to have to work with this, and I
want to minimize the amount of slapping-people-around-when-they-don't-
read-my-documentation that I have to do -- assuming I'm still even
working on the project.
to allow it).
half of the singleton model. E.g., making the constructors public in
addition to a static getInstance() wouldn't cause any harm.