Re: Non-static singleton setup to control life time
On 15 Dec, 00:29, Francis Glassborow
<francis.glassbo...@btinternet.com> wrote:
Kris Prad wrote:
I want to setup a singleton, without 'static' initialization.
What is wrong with this:
class Single {
public:
Single(){
if(exists) throw("You can only have one object of type Single");
exists = true;
// do rest
}
~Single() { exists = false }
// rest of interface
private:
static bool exists;
// rest of private interface etc.
};
bool Single::exists(false);
This allows a Single (which is a singleton) to be created whilst
avoiding statics. But it doesn't show a Single actually being created.
I think the OP is also concerned with the lifetime of the singleton,
i.e who creates it and when does it get destroyed.
For complex systems that use many singletons where initialisation
order is important, a singleton manager is sometimes used. The manager
is usually instantiated in main, then register functions called for
the various singletons that are needed. Singleton destruction occurs
in reverse order of registration. FWIW, ACE has such a singleton
manager.
Regards,
Andrew Marlow
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]