Re: Automagic instantiation of singletons? (Christian Brunschen)
Re: Automagic instantiation of singletons? (Christian Brunschen)
- Subject: Re: Automagic instantiation of singletons? (Christian Brunschen)
- From: Theo Vosse <email@hidden>
- Date: Fri, 18 Nov 2005 16:36:39 +0100
The common way in C++ for instantiating singletons is
class Singleton
{
public:
Singleton()
{
// Instantiation code here
}
};
static Singleton my_singleton_instantiation_dont_touch;
The static variable will cause Singleton::Singleton() to be called
before main() is executed. You can mix C++ and Objective-C code
without problems. There are (AFAIK) only problems with this trick
when loading dynamic libraries in a multi-threaded context.
A more Cocoa-like way of doing it might be to use +initialize, but it
doesn't seem to be able to do general initializations (as there is no
guarantee to when it is called other than "before the first message
to an object of the specific class").
Anyway, the perfect place for doing initializations in a Cocoa
application is in main(), just before the call to NSApplicationMain().
Theo
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden