I would use a singleton:
Singleton* single = Singleton::instance(); single->do_it();
I would use an unnamed class:
single.do_it();
It seems to me that the Singleton pattern has no advantage over an unnamed class, except for reading error messages. Using singletons is more inconvenient than using an unnamed class object. First, clients must first obtain an instance handle; secondly, the Singleton::instance() developer may need to consider concurrency.
So why and how did you choose a singleton over an unnamed class?
In addition, although the obvious definition of an unnamed class may be
class {
I could also define it like this:
#ifndef NDEBUG class Singleton__ { // readable error messages, #else class { // unnamed, clients can't instantiate #endif // ... }single;
the latter approach has the advantage of reading compiler error messages, but is not single in debug mode.
c ++ singleton
wilhelmtell Dec 28 '08 at 1:58 2008-12-28 01:58
source share