Re: Newbie question - is where Obj-C analog for a C++ static data members?
Re: Newbie question - is where Obj-C analog for a C++ static data members?
- Subject: Re: Newbie question - is where Obj-C analog for a C++ static data members?
- From: Neil Earnshaw <email@hidden>
- Date: Wed, 10 Sep 2003 09:19:20 +0100
Two solutions:
1. Use a static variable in the class' .m file:
#import "MyClass.h"
static MyClass* _instance;
@implementation MyClass
+(void)initialize
{
_instance = [[MyClass alloc] init];
}
...
@end
+initialize will be called by the Obj-C runtime before the class is
used.
2. Use a static in a class accessor method:
+(MyClass*)defaultInstance
{
static MyClass* _instance;
if ( !_instance ) {
_instance = [[MyClass alloc] init];
}
return _instance;
}
It's the same as C really. Classes don't have explicit static data
members.
Check out the "Objective-C Pocket Reference" from O'Reilly. It's a
little gem.
-Neil
On Wednesday, September 10, 2003, at 08:23 AM, Rustam Muginov wrote:
Hello all.
I have a document-based applications, with several windows where
different
objects are drawn.
I need to draw them with the different colors, depending on the object
"type" or "kind".
So I created the NSDictionary, with the colors as objects and object
types
as the keys.
The questions is - how could I optimize my code and not the build such
colors dictionary for every WindowController (or for every document)?
In C++, I could declare the static data member for the class, init it
with
NULL value, and then check it from the instance method, building the
color
list if its not already build.
In Cocoa, I could probably define the "standalone" global variable and
use
it for the same purposes, but I feel this is not very "cocoa-ish"
How should I implement "shared" object, which will be inited once and
then
used by many instances?
_______________________________________________
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.
Neil Earnshaw
Consultant Software Engineer
Object Software Engineers Ltd
email@hidden
Tel : 01747 854 842
Mbl : 07870 209 102
_______________________________________________
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.