home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:19653 gnu.g++.help:1684
- Newsgroups: comp.lang.c++,gnu.g++.help
- Path: sparky!uunet!spool.mu.edu!enterpoop.mit.edu!bloom-picayune.mit.edu!fritz
- From: fritz@mtl.mit.edu (Frederick Herrmann)
- Subject: References to functions?
- Message-ID: <1993Jan21.044013.1429@athena.mit.edu>
- Followup-To: comp.lang.c++
- Sender: news@athena.mit.edu (News system)
- Nntp-Posting-Host: wayne.mit.edu
- Organization: MIT Microsystems Technology Laboratories
- Date: Thu, 21 Jan 1993 04:40:13 GMT
- Lines: 51
-
- 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).
-
- Here's what I was trying to do:
-
- typedef void (&rfi)(int);
- typedef void (*pfi)(int);
-
- class X {
- public:
- rfi f;
-
- X( pfi ff) : f( *ff) {};
-
- void some_member_func( int i) { f(i); }
- };
-
- Looks reasonable to me, except I can hear the grumbling that f is not
- properly initialized, since ff could be a dangling pointer. On the
- other hand, g++ lets me get away with this:
-
- class Y {
- public:
- int& ri;
-
- Y( int* pi) : ri( *pi) {};
- };
-
- which has the same problem when I construct Y(0).
-
- Now ARM 8.4.3 says a reference must be initialized by an "object", and
- if a function is not an object then class X is out. But the last
- paragraph of 8.2.2 gives a long list of things that can't be
- referenced, and functions are not listed.
-
- It's not a big deal to me, since it's really a matter of notational
- convenience. I'd be grateful for some discussion; can someone tell me:
-
- * A place in the ARM or other source where this is more clearly
- forbidden.
-
- * An explanation of implementation issues or other reasons why
- function references are a bad idea.
-
- * Whether other compilers can handle class Y above; is this a g++ bug?
-
- Thanks,
-
- - Fritz
- fritz@mtl.mit.edu
-