• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Automagic instantiation of singletons? (Christian Brunschen)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Automagic instantiation of singletons? (Christian Brunschen)
      • From: Greg Herlihy <email@hidden>
  • Prev by Date: Re: Automagic instantiation of singletons?
  • Next by Date: Re: Automagic instantiation of singletons?
  • Previous by thread: Re: Using keyboard to toggle checkboxes in NSTableView cell
  • Next by thread: Re: Automagic instantiation of singletons? (Christian Brunschen)
  • Index(es):
    • Date
    • Thread