references - C++

karl at haddock karl at haddock
Wed Sep 10 03:53:00 AEST 1986


alice!bs (Bjarne Stroustrup) writes:
>Karl W. Z. Heuer writes:
>>BMS-AT!stuart writes:
>>>[re the declaration of reference types "foo(char &c)"]
>>>I don't like this.  It violates the nice consistent way ...
>>
>>I am also somewhat uneasy about calling it "char &c".  The consistent way to
>>declare it would be "char *&c", since you have to write "&c" to get ahold of
>>the "char *" object you are really using.
>
>The notation T& is the C++ notation for a reference to an object of type T.
>        char& c;        declares c to be a reference to a char
>        char*& r;       declares r to be a reference to a char*
>& is a declarator operator exactly like *

Understood.  The point was that (in C) a declaration like "int *(*f[])();"
asserts that "*(*f[])()" is an int (so one can work backwards to show that
f must be an array of pointers to functions returning pointers to int).  The
use of "&" for the reference declarator operator breaks this heuristic; now
"char &c" declares a reference to a "char", so "&c" is of type "char *".  If
the operator were spelled "*&" instead, then "char *&c" would denote a
reference to a "char", "char **&r" would be a reference to a "char *", and
"char &x" would be illegal.  Since "**&r" really does give you an object of
type "char", this notation might be easier to interpret.  (I suspect that
stuart was (incorrectly) applying this heuristic, and it led him to believe
that "&c" was somehow an object of type "char".)

Karl W. Z. Heuer (ima!haddock!karl; karl at haddock.isc.com)
This isn't C (not even ANSI proposed), so let's move it to net.lang.c++, OK?



More information about the Comp.lang.c mailing list