Re: declaring instance variable to be a function
Re: declaring instance variable to be a function
- Subject: Re: declaring instance variable to be a function
- From: James Bucanek <email@hidden>
- Date: Sat, 22 Jul 2006 07:48:35 -0700
Roland Silver wrote on Saturday, July 22, 2006:
>In an @interface declaration, how do I declare an instance variable
>foo to be a function with no arguments returning int?
>I've tried
>(int (void))foo;
>and
>int (void)*foo;
>which yield the error message
>parse error before '(' token
>and
>int (void)foo;
>and
>int (void)*foo;
>which yield
>parse error before 'void'
Function pointer declarations in C are tricky to say the least. My typical solution is to find a working example (NSArray sortedArrayUsingFunction:context: is a good one) and just copy, paste, and edit.
Here's what you want:
@interface IntFunc : NSObject
{
int (*foo)(void);
}
- (id)initWithFooFunction:(int (*)(void))functionPtr;
@end
@implementation IntFunc
- (id)initWithFooFunction:(int (*)(void))functionPtr
{
if ( (self=[super init]) != nil )
{
foo = functionPtr;
}
return (self);
}
@end
--
James Bucanek
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden