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

  1. Newsgroups: comp.sys.next.programmer
  2. Path: sparky!uunet!utcsri!torn!news.ccs.queensu.ca!news
  3. From: alastair@hecate.phy.queensu.ca (A.B. McLean)
  4. Subject: Re: Pointers to functions in method definitions
  5. Message-ID: <C0HzH1.JAq@knot.ccs.queensu.ca>
  6. Sender: news@knot.ccs.queensu.ca (Netnews control)
  7. Organization: Queen's University, Kingston
  8. References: <C0CAtI.KqM@knot.ccs.queensu.ca>
  9. Date: Thu, 7 Jan 1993 19:00:37 GMT
  10. Lines: 104
  11.  
  12. In article <C0CAtI.KqM@knot.ccs.queensu.ca> alastair@hecate.phy.queensu.ca  
  13. (A.B. McLean) writes:
  14. > POINTERS TO FUNCTIONS IN METHOD DEFINITIONS
  15. > I want to write a method that will accept a pointer to a C function.
  16. > For example, let the function be a simple Gaussian:-
  17. > void gaussian(x,a,y)
  18. > float x,a[],*y;
  19. > {
  20. >   float arg;
  21. >   
  22. >   arg = (x-a[2])/a[3];
  23. >   *y += a[1]*exp(-arg*arg);
  24. > }
  25. > In the interface file I would expect (Kernigham amd Ritchie section  
  26. 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. Thanks to everyone who replied to my pointer to a function problem. 
  43. Here is a summary of the replies:-the syntax given below works..
  44. I tried them !
  45. _______________________________________________________
  46.  
  47. No, types of arguments to Objective C methods are not specified in the
  48. same way as types of arguments to regular C functions.  Rather, they are
  49. specified in C "cast" format -- e.g., -method:(int)i; not -method:int i;
  50.  
  51. For your example:
  52.  
  53. - methodName:(void(*)(float, float*, float*))functionName;
  54.  
  55. -- 
  56. Ray Spalding
  57. ray.spalding@oit.gatech.edu (NeXT Mail accepted)
  58. ________________________________________________________
  59.  
  60. The syntax for specifying a function pointer is wrong.  Try the following:
  61.  
  62. - methodName: (void (*)(float, float*, float*))gaussian;
  63.  
  64. Or, even better
  65.  
  66. typedef  void (*GaussianFunc)(float, float*, float*);
  67.  
  68. - methodName: (GaussianFunc)gaussian;
  69.  
  70.  
  71. --
  72. Frank Mitchell
  73. frank@fnbc.com (NeXTmail)
  74. ________________________________________________________
  75.  
  76. The best solution might be that to use "typedef".  Simply, you define
  77. a GaussianType as:
  78.  
  79. typedef void (*GaussianType)(float, float *, float *);
  80.  
  81. and then declare
  82.  
  83. - mothodName : (GaussianType) handler
  84. {
  85.  // ...
  86.  return self;
  87. }
  88.  
  89. and that will do.
  90.  
  91. ken
  92. zhao@acsc.com
  93. ________________________________________________________
  94. Try
  95.  
  96. - methodName:(void (*gaussian)(float, float*, float*)));
  97.  
  98. Perhaps this will work (types have to be in parenthesis).
  99.  
  100. Marcel
  101. waldvoge@nessie.cs.id.ethz.ch (Marcel Waldvogel)
  102. ________________________________________________________
  103. Thanks 
  104. Alastair McLean
  105. alastair@hecate.phy.queensu.ca
  106.  
  107.