Does anyone have an idea why the ampersand was chosen as the way to denote references in C++?
AFAIK (though I don't have the book near me), Stroustroup didn't explain that choice, which I find a little odd because the same symbol was already used for address-of in C.
Stroustrup was always very reluctant to introduce a new reserved symbol or name, so he probably used it to avoid making the feature look weird to users of C.
In addition to Earwicker's response which I generally agree with. I would also speculate that since & is the "address-of" operator, it is somewhat fitting. Since a reference in many ways is like passing by address instead of by value.
In addition to that, taking the address of a variable is often referred to as "referencing"
(Yes I know that references don't have to be implemented using pointers under the hood, I am referring to the way they conceptually work).
This is just speculation though.
Who knows why Stroustrup does anything, but my guess is that because the implementation of reference parameters involves passing the address of an lvalue, Stroustrup chose the C address-of operator because it would give C programmers the right idea about the cost model.
Here is my theory on that. I think it has much to do with what operators are valid (syntactically) for symbols. Consider
int a[1]; // a[1] is valid (syntactically)
int *a; // *a is valid
int a(char, bool); // a(<a char>, <a bool>) is valid (function call)
int C::*a; // <a C>.*a is valid
Conceptually, in those declarations what is named with a type (C
, char
, bool
) is substituted with an expression of that type later on. Of course the intention is to reuse as much of the existing language as possible. So i think he used &
:
int &a; // &a is valid
The important one is that &
is only valid on the kind of expression a reference denotes: For lvalues
. References are lvalues (named variables are too) and only for them &
can be applied:
int &g(); // &g() is valid (taking the address of the referred to integer)
int g(); // &g() is *not* valid (can't apply to temporary int)
My thought was that there are 2 symbols used in pointers: * and &.
since int* means a pointer to an int, probably Stroustrup didn't want to introduce a whole new symbol. Since references are sort of like pointers, he stuck with &. Plus, the only previously valid use of & was to take the address of something, so it was OK to use in declarations.