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

家住魔仙堡 提交于 2019-12-03 08:58:36

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!