Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was the ampersand chosen as the symbol for references in C++? [closed]

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.

like image 693
Uri Avatar asked Mar 21 '09 22:03

Uri


People also ask

Why do we use ampersand 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.

What is the purpose of the & symbol?

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.

Which symbol is used to reference pointers?

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.

What does reference mean in C?

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.


1 Answers

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.

like image 171
Evan Teran Avatar answered Sep 24 '22 12:09

Evan Teran