🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.sys.next.programmer
5 messages
5 total messages Started by A.B. McLean Mon, 04 Jan 1993 17:20
Pointers to functions in method definitions
#2176
Author: A.B. McLean
Date: Mon, 04 Jan 1993 17:20
35 lines
1205 bytes
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
Re: Pointers to functions in method definitions
#2185
Author: Gordie Freedman
Date: Tue, 05 Jan 1993 08:22
62 lines
1986 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

This should do it:

- methodName:(void (*) (float, float*, float*)) gaussian
{
	return self;
}

Hope this works for you, I just tried this:

- doit : (void (*) (int, int)) foo :(int) x :(int) y
{
	// do something
}

in some obj-c code and it worked.

Remember, the syntax for declaring parameters to obj-c methods is different  
than declaring parameters for C functions, objc uses a "cast"-like syntax for  
parameter declarations in methods.

-- 

--- Gordie Freedman gordie@cyclesoft.com  NeXTMail Yes! ---
>>> Thou shalt not inline functions more complicated than 20 
Re: Pointers to functions in method definitions
#2194
Author: Sam Streeper
Date: Wed, 06 Jan 1993 05:10
23 lines
1142 bytes
alastair@hecate.phy.queensu.ca (A.B. McLean) writes:
> I want to write a method that will accept a pointer to a C function.


I would guess that this isn't directly possible; it looks like the Objective-C  
runtime doesn't have the ability to encode data of type function pointer.

A couple of solutions might include passing the function pointer as an int  
pointer and then casting it to a function pointer, or to remove the function  
interface and pass a selector:

- invokeThisMethod:(SEL) aMethod
{
	[self perform: aMethod with:self];

}

If aMethod = @selector(foo:), this is roughly equivalent to [self foo:self].

-sam
--
Opinions expressed herein are not those of my employer.  They're not even
mine.  They're probably wrong besides.  How did they get in here, anyway?
Re: Pointers to functions in method definitions
#2225
Author: A.B. McLean
Date: Thu, 07 Jan 1993 19:00
103 lines
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
Re: Pointers to functions in method definitions
#2290
Author: Dwight Everhart
Date: Mon, 11 Jan 1993 02:46
20 lines
1300 bytes
In article <C0CAtI.KqM@knot.ccs.queensu.ca> alastair@hecate.phy.queensu.ca  
(A.B. McLean) writes:
> I want to write a method that will accept a pointer to a C function.
[...]
> 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*);

In Objective-C method declarations, all types must be given in parenthesis,  
using the C type-cast syntax.  Also, the name of the argument is given after  
its type.  So what you want is:

- methodName:(void(*)(float, float*, float*))gaussian;

--
Dwight Everhart          "Technology turned out to be less important than
Houston, Texas            sales and distribution methods ... we consistently
biff@biff.gbdata.com      outsold people who had better technology."
NeXTMail and MIME OK      -- Thomas J. Watson Jr., on the success of IBM
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads