Skip navigation.
 
mlRe: declaring instance variable to be a function
FROM : James Bucanek
DATE : Sat Jul 22 16:48:35 2006

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

Related mailsAuthorDate
mldeclaring instance variable to be a function Roland Silver Jul 22, 16:21
mlRe: declaring instance variable to be a function James Bucanek Jul 22, 16:48