Re: what's wrong with the following singleton class???
werasm wrote:
If single controlled access to a unique instance, and not "creation on
demand" is the reason for the singleton,
From what I see of the use of singletons, the convenience is being able
to use it without having to pass the reference to it through the class
header, thus supposedly hiding the implementation detail.
I circumvent this by using either the pImpl paradigm or having an
abstract base class and a factory, so users of your class (or its
abstact base) do not need to see how you implement.
With a lot of singletons I come across, although there is only one
instance, there is no particular reason why there couldn't be more than
one - for example a database. Why shouldn't an application interact
with more than one database?
Having an "open the first time on use" does not imply singleton, it
implies an equivalent to lazy-evaluation.
How to handle the re-entrant and mulitple-creation situation is down to
the specifics of the application. If you want multiple connections to a
database each running in different threads, then the best option is to
open the database first just before you create a connection pool. You
ensure this by designing your classes well.