Article View: comp.sys.next.programmer
Article #2225Re: Pointers to functions in method definitions
From: A.B. McLean
Date: Thu, 07 Jan 1993 19:00
Date: Thu, 07 Jan 1993 19:00
103 lines
3026 bytes
3026 bytes
In article <C0CAtI.KqM@knot.ccs.queensu.ca> alastair@hecate.phy.queensu.ca
(A.B. McLean) writes:
> POINTERS TO FUNCTIONS IN METHOD DEFINITIONS
>
> I want to write a method that will accept a pointer to a C function.
> For example, let the function be a simple Gaussian:-
>
> void gaussian(x,a,y)
> float x,a[],*y;
> {
> float arg;
>
> arg = (x-a[2])/a[3];
> *y += a[1]*exp(-arg*arg);
> }
>
> In the interface file I would expect (Kernigham amd Ritchie section
5.11)
> the method definition to look like
>
> - methodName:void(*gaussian)(float, float*, float*);
>
> and in the implementation file I may have
>
> - methodName:void(*gaussian)(float, float*, float*)
> {
> // ...
> return self;
> }
>
> When I try to do this the compiler gives me the message
>
> parse error before `void'
>
> Any suggestions ?
>
> Alastair McLean
> alastair@hecate.phy.queensu.ca
Thanks to everyone who replied to my pointer to a function problem.
Here is a summary of the replies:-the syntax given below works..
I tried them !
_______________________________________________________
No, types of arguments to Objective C methods are not specified in the
same way as types of arguments to regular C functions. Rather, they are
specified in C "cast" format -- e.g., -method:(int)i; not -method:int i;
For your example:
- methodName:(void(*)(float, float*, float*))functionName;
--
Ray Spalding
ray.spalding@oit.gatech.edu (NeXT Mail accepted)
________________________________________________________
The syntax for specifying a function pointer is wrong. Try the following:
- methodName: (void (*)(float, float*, float*))gaussian;
Or, even better
typedef void (*GaussianFunc)(float, float*, float*);
- methodName: (GaussianFunc)gaussian;
--
Frank Mitchell
frank@fnbc.com (NeXTmail)
________________________________________________________
The best solution might be that to use "typedef". Simply, you define
a GaussianType as:
typedef void (*GaussianType)(float, float *, float *);
and then declare
- mothodName : (GaussianType) handler
{
// ...
return self;
}
and that will do.
ken
zhao@acsc.com
________________________________________________________
Try
- methodName:(void (*gaussian)(float, float*, float*)));
Perhaps this will work (types have to be in parenthesis).
Marcel
waldvoge@nessie.cs.id.ethz.ch (Marcel Waldvogel)
________________________________________________________
Thanks
Alastair McLean
alastair@hecate.phy.queensu.ca
Technical Details
Message-ID:
<C0HzH1.JAq@knot.ccs.queensu.ca>Path:
arc1-ca.pugleaf.net!sparky!uunet!utcsri!torn!news.ccs.queensu.ca!newsReferences:
<C0CAtI.KqM@knot.ccs.queensu.ca>Show Full Headers
Complete Article Headers
Newsgroups: comp.sys.next.programmer Path: arc1-ca.pugleaf.net!sparky!uunet!utcsri!torn!news.ccs.queensu.ca!news From: alastair@hecate.phy.queensu.ca (A.B. McLean) Subject: Re: Pointers to functions in method definitions Message-ID: <C0HzH1.JAq@knot.ccs.queensu.ca> Sender: news@knot.ccs.queensu.ca (Netnews control) Organization: Queen's University, Kingston References: <C0CAtI.KqM@knot.ccs.queensu.ca> Date: Thu, 7 Jan 1993 19:00:37 GMT Lines: 104