home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: References to functions?
- Message-ID: <1993Jan21.173352.10406@taumet.com>
- Organization: TauMetric Corporation
- References: <1993Jan21.044013.1429@athena.mit.edu>
- Date: Thu, 21 Jan 1993 17:33:52 GMT
- Lines: 41
-
- fritz@mtl.mit.edu (Frederick Herrmann) writes:
-
- >I tried to declare a reference to a function, but found that g++
- >wouldn't let me. After scanning ch. 8 of the ARM, I interpret it to
- >forbid function references, but it's not all that clear (see below).
-
- References to functions are legal, but not very useful. I can't
- think of anything you can do with a reference to a function you
- can't do with a const pointer to a function in exactly the same way,
- except for trivial syntax differences at the point of initialization.
- You use them in exactly the same way.
-
- int foo(int);
-
- typedef int (&fooref)(int); // reference to function
- typedef int (*fooptr)(int); // pointer to function
-
- fooref fr = *foo;
- fooptr const fp = foo;
-
- int j = fr(3); // call function via reference
- int i = fp(2); // call function via pointer
-
- int bar(int);
- fr = *bar; // error, can't assign to a reference
- fp = bar; // error, can't assign to a const pointer
-
- class Xr {
- fooref frm;
- Xr(fooref f) : frm(f) { }
- Xr() { } // error, reference member not initialized
- };
-
- class Xp {
- fooptr const fpm;
- Xp(fooptr f) : fpm(f) { }
- Xp() { } // error, const member not initialized
- };
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
-