Re: ObjC static member variable
Re: ObjC static member variable
- Subject: Re: ObjC static member variable
- From: Chris Hanson <email@hidden>
- Date: Thu, 10 Apr 2008 17:17:04 -0700
On Apr 10, 2008, at 12:42 PM, vance wrote:
Yes that works but then it becomes a global variable.
I was wondering if ObjC can handle static member variables. If not I
will have to hack it up and declare the variable outside of the class
A C++ static member variable is a global variable too, its scope is
just managed by its declaring class.
Objective-C only has direct support for instance variables.
You can implement either global-for-all-subclasses class variables or
unique-per-subclass class variables yourself using class methods and
either a single file-static variable (for a global-for-all-subclasses
class variable) or a file-static map table keyed by class (for a
unique-per-subclass class variable).
However, as with all global variables, think long and hard about
whether you really need them and how they will be used. Often the
implementation of a "singleton" design pattern is a code smell, gets
in the way of comprehensive unit testing and makes test-driven
development and refactoring harder than it should be.
-- Chris
_______________________________________________
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