Re: Confused about a thread-safe singleton example.
On Dec 4, 4:58 pm, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
On Dec 4, 12:10 pm, James Kanze <james.ka...@gmail.com> wrote:
On Dec 3, 10:25 pm, Maxim Yegorushkin
A global variable by definition is a singleton, is it not?
No. A singleton is a type, not a variable. What
characterizes a type as a singleton is that there can only
be zero or one instances of the type in a program. The
restriction on the number of instances is part of the
abstraction of the type.
I see now, it is about enforcing the number of instances on
Yes. In the case of C++, it's also often about managing order
of initialization issues, but that's really more of a pragmatic
consideration than something fundamental to the concept of a
So, a global variable can be an instance of a singleton class,
can it not?
It would depend on how the singleton is implemented. The
oldest, traditional implementation (use a static counter,
incremented in the constructor, and assert that it is never
greater than 1 after the incrementation) certainly allows it.
Most of the C++ implementations wouldn't allow it, requiring you
to go through a special function. More generally, I think, this
is usually the case in other languages as well, since by making
the constructor private, and requiring the function to be
called, makes it impossible to write compilable code which would
instantiate more than one instance.
It's not a perfect solution, however---as I think you
mentionned, it doesn't allow derivation, at least not easily.
And all those calls to instance() ARE a real pain in the neck.
James Kanze (GABI Software) email:firstname.lastname@example.org
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34