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.
The ampersand is the address of operator. It returns the memory location of a variable and that's the only way it's used, prefixed to a variable like the engine on a train.
An ampersand (&) is a typographical symbol that is rarely used in formal writing. It is read aloud as the word and and is used as a substitute for that word in informal writing and in the names of products or businesses.
The syntax of declaring a pointer is to place a * in front of the name. A pointer is associated with a type (such as int and double ) too.
A reference is the object, just with another name. It is neither a pointer to the object, nor a copy of the object. It is the object.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With