• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: declaring instance variable to be a function
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >declaring instance variable to be a function (From: Roland Silver <email@hidden>)

  • Prev by Date: Re: Root launched daemons and distributed objects clash?
  • Next by Date: Finding out if an NSArray's contents are all equal
  • Previous by thread: declaring instance variable to be a function
  • Next by thread: Finding out if an NSArray's contents are all equal
  • Index(es):
    • Date
    • Thread