Re: Singletons?
Re: Singletons?
- Subject: Re: Singletons?
- From: Gregory Weston <email@hidden>
- Date: Fri, 21 Jun 2002 17:23:43 -0400
On 6/21/02 at 8:05 AM, Christian Brunschen <email@hidden> wrote:
>
A static variable is only visible in file scope - i.e., only in the file
>
where it is defined. So a static variable declared & defined in the class
>
implementation is not visible to anything outside that class. Thus, there
>
is no pollution of global namespace, nor can anyone else refer to the
>
variable and change it from underneath you.
>
>
@implementation Foo
>
>
static Foo *sharedInstance = nil;
>
>
+ sharedInstance {
>
if (sharedInstance == nil) {
>
sharedInstance = [[self alloc] init];
>
}
>
>
return sharedInstance;
>
}
>
>
// ... rest of class ...
>
>
@end
>
>
.... will work fine.
Also note that the variable can be moved inside the sharedInstance method:
+ (id)sharedInstance
{
static Foo* sSharedInstance = nil;
if (sharedInstance == nil)
{
sharedInstance = [[self alloc] init];
}
return sharedInstance;
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.