home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19680 < prev    next >
Encoding:
Text File  |  1993-01-21  |  1.5 KB  |  52 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: References to functions?
  5. Message-ID: <1993Jan21.173352.10406@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <1993Jan21.044013.1429@athena.mit.edu>
  8. Date: Thu, 21 Jan 1993 17:33:52 GMT
  9. Lines: 41
  10.  
  11. fritz@mtl.mit.edu (Frederick Herrmann) writes:
  12.  
  13. >I tried to declare a reference to a function, but found that g++
  14. >wouldn't let me.  After scanning ch. 8 of the ARM, I interpret it to
  15. >forbid function references, but it's not all that clear (see below).
  16.  
  17. References to functions are legal, but not very useful.  I can't
  18. think of anything you can do with a reference to a function you
  19. can't do with a const pointer to a function in exactly the same way,
  20. except for trivial syntax differences at the point of initialization.
  21. You use them in exactly the same way.
  22.  
  23. int foo(int);
  24.  
  25. typedef int (&fooref)(int); // reference to function
  26. typedef int (*fooptr)(int); // pointer   to function
  27.  
  28. fooref       fr = *foo;
  29. fooptr const fp = foo;
  30.  
  31. int j = fr(3);    // call function via reference
  32. int i = fp(2);    // call function via pointer
  33.  
  34. int bar(int);
  35. fr = *bar;    // error, can't assign to a reference
  36. fp = bar;    // error, can't assign to a const pointer
  37.  
  38. class Xr {
  39.     fooref frm;
  40.     Xr(fooref f) : frm(f) { }
  41.     Xr() { } // error, reference member not initialized
  42. };
  43.  
  44. class Xp {
  45.     fooptr const fpm;
  46.     Xp(fooptr f) : fpm(f) { }
  47.     Xp() { } // error, const member not initialized
  48. };
  49. -- 
  50.  
  51. Steve Clamage, TauMetric Corp, steve@taumet.com
  52.