home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sys / next / programm / 7986 < prev    next >
Encoding:
Text File  |  1993-01-05  |  1.9 KB  |  76 lines

  1. Newsgroups: comp.sys.next.programmer
  2. Path: sparky!uunet!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!yamanote!gordie
  3. From: gordie@cyclesoft.com (Gordie Freedman)
  4. Subject: Re: Pointers to functions in method definitions
  5. Message-ID: <1993Jan5.082236.23079@netcom.com>
  6. Sender: gordie@netcom.com
  7. Reply-To: gordie@cyclesoft.com
  8. Organization: Dolphin Software
  9. References: <C0CAtI.KqM@knot.ccs.queensu.ca>
  10. Date: Tue, 5 Jan 1993 08:22:36 GMT
  11. Lines: 63
  12.  
  13. In article <C0CAtI.KqM@knot.ccs.queensu.ca> alastair@hecate.phy.queensu.ca  
  14. (A.B. McLean) writes:
  15. > POINTERS TO FUNCTIONS IN METHOD DEFINITIONS
  16. > I want to write a method that will accept a pointer to a C function.
  17. > For example, let the function be a simple Gaussian:-
  18. > void gaussian(x,a,y)
  19. > float x,a[],*y;
  20. > {
  21. >   float arg;
  22. >   
  23. >   arg = (x-a[2])/a[3];
  24. >   *y += a[1]*exp(-arg*arg);
  25. > }
  26. > In the interface file I would expect (Kernigham amd Ritchie section 5.11)
  27. > the method definition to look like 
  28. > - methodName:void(*gaussian)(float, float*, float*);
  29. > and in the implementation file I may have
  30. > - methodName:void(*gaussian)(float, float*, float*)
  31. > {
  32. >   // ...
  33. >   return self;
  34. > }
  35. > When I try to do this the compiler gives me the message
  36. >   parse error before `void'
  37. >   
  38. > Any suggestions ?
  39. > Alastair McLean
  40. > alastair@hecate.phy.queensu.ca
  41.  
  42. This should do it:
  43.  
  44. - methodName:(void (*) (float, float*, float*)) gaussian
  45. {
  46.     return self;
  47. }
  48.  
  49. Hope this works for you, I just tried this:
  50.  
  51. - doit : (void (*) (int, int)) foo :(int) x :(int) y
  52. {
  53.     // do something
  54. }
  55.  
  56. in some obj-c code and it worked.
  57.  
  58. Remember, the syntax for declaring parameters to obj-c methods is different  
  59. than declaring parameters for C functions, objc uses a "cast"-like syntax for  
  60. parameter declarations in methods.
  61.  
  62. -- 
  63.  
  64. --- Gordie Freedman gordie@cyclesoft.com  NeXTMail Yes! ---
  65. >>> Thou shalt not inline functions more complicated than 20 
  66.  
  67.