Re: Pointers to methods (or functions, for that matter)
Re: Pointers to methods (or functions, for that matter)
- Subject: Re: Pointers to methods (or functions, for that matter)
- From: Jeff LaMarche <email@hidden>
- Date: Thu, 18 Oct 2001 22:08:11 -0700
I can't seem to remember how to create a pointer to a function in C.
I'm
fairly sure it is a C capability... How would I do it with a method in
ObjC?
For methods in Objective C, just use a selector
SEL mySelector = @selector(methodName:);
They can be taken as parameters to methods and are more flexible because
the address is determined at run time, not at compile time.
As for C, if you wanted to declare a pointer to a function that took an
int and a char and returns an int, you would declare it like this
int (*pt2Function) (float, char, char);
Then you could assign a function to it in one of these two ways:
int DoIt (float a, char b, char c){ printf("DoIt\n"); return
a+b+c; }
int DoMore(float a, char b, char c){ printf("DoMore\n"); return a-
b+c; }
pt2Function = DoMore; // assignment
pt2Function = &DoIt; // alternative using address
operator
These examples come from a detailed FAQ on function pointers in C and
C++ at:
http://www.function-pointer.org/
(courtesy of the almighty google - it's a good place to look for answers
to generic programming questions like this - frankly, I couldn't
remember myself =) )
Jeff
On Thursday, October 18, 2001, at 09:31 PM, Isaac Sherman wrote: