Re: Apples's code examples... It is me
Re: Apples's code examples... It is me
- Subject: Re: Apples's code examples... It is me
- From: Jean-Daniel Dupas <email@hidden>
- Date: Wed, 6 Feb 2008 15:55:18 +0100
Le 6 févr. 08 à 15:10, Yanko Ivanov a écrit :
Kyle, I did notice the "static" keyword. However the it is still
confusing for me (Java programmer).
Stéphane, yes it is me.
I should have made a test (http://pastie.textmate.org/148207) before
posting :)
Regards,
Yanko
if you come from the java world, you probably know the singleton
pattern:
class MyClass {
static MyClass _sharedInstance;
public static MyClass sharedInstance() {
if (_sharedInstance)
return _sharedInstance;
/* put multithread support here (synchronize(), etc...) */
_sharedInstance = new MyClass();
return _sharedInstance;
}
protected MyClass() {
/* protected constructor */
}
}
In the Apple sample code, this is the same pattern, but static is not
exactly the same in C than in Java. a static variable is stored in the
heap and does not each from one call to another.
a Static variable initialization is perform only one time, so the
"static id myObject = nil;" is not perform on each call by only once
at program startup.
(From Wikipedia: In the C programming language, static is used with
global variables and functions to set their scope to the containing
file. In local variables, static is used to store the variable on the
heap instead of in the transient call stack.)_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden