Re: Obj-C language question: C function within @implementation
Re: Obj-C language question: C function within @implementation
- Subject: Re: Obj-C language question: C function within @implementation
- From: Dietmar Planitzer <email@hidden>
- Date: Thu, 3 Jul 2003 20:43:20 +0200
Hi,
functions inside an ObjC class implementation block are allowed to
directly access all instance variables of the class in question, no
matter if the instance variable was declared @private, @protected or
@public. In that sense the function becomes a part of the ObjC class.
However technically the function remains to be a good old C function.
Functions outside a class implementation block are subject to the
instance variable protection status check.
There are some good uses for such "class functions": They make it
possible to put performance critical code into a function and thus call
it without the much higher method call overhead. Further, they are
quite handy whenever you need to pass a function as a callback to
another API and still want to be able to access private instance
variables of a class.
Regards,
Dietmar Planitzer
On Thursday, July 3, 2003, at 03:44 PM, Sean McBride wrote:
I'm no Obj-C expert, but I found some suspicious code that my Obj-C
newbie coworker wrote. I've reduced it to this example:
#import <Cocoa/Cocoa.h>
int main(int argc, const char *argv[])
{
return NSApplicationMain(argc, argv);
}
@interface MyClass : NSObject
{
IBOutlet NSProgressIndicator* progress;
}
@end
@implementation MyClass
void Function (void* thing)
{
MyClass* uc = (MyClass*)thing;
[uc->progress stopAnimation:uc];
}
@end
Notice that there is a C function within the @implementation block.
This
compiles without error/warning with gcc and CodeWarrior. If I move
this
outside the @implementation block CW gives an error and gcc gives a
warning (progress is protected), both of which make more sense IMHO.
Should this code compile as it is above? Seems to me progress is
protected no matter where Function() is.
Thanks!
--
____________________________________________________________
Sean McBride, B. Eng email@hidden
Mac Software Designer +1-514-822-6000
Matrox Electronic Systems Ltd. Montrial, Quibec, Canada
_______________________________________________
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.
_______________________________________________
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.