What is a reference variable in C++?

2018-12-31 12:29发布

What would be a brief definition of a reference variable in C++?

13条回答
不再属于我。
2楼-- · 2018-12-31 12:48

Page 79 ~ 80 C++ Primer 5th ed.

Object: A region of memory that has a type

Variable: Named object or reference

Reference: An alias for another object.

查看更多
临风纵饮
3楼-- · 2018-12-31 12:54

The first paragraph of the Wikipedia article could easily serve as a brief definition:

In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C.

And quoting from the same article:

C++ references differ from pointers in several essential ways:

  • It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references.

  • Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.

  • References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid.

  • References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor.

Further reading:

查看更多
妖精总统
4楼-- · 2018-12-31 12:55

Reference variables (let a), just say for easy understanding, another name of variable (let x), which holds the same exact memory location as that of x.

int &a = x; refers that a holds same memory location as that of x.

For example, say two roommates share the same room. One friends name is x and another friends name is a. If a changes the location of the table placed in the room, from position (x,y,z) to (x1,y1,z1) then changes are visible to friend x as well and vice versa.

查看更多
爱死公子算了
5楼-- · 2018-12-31 12:56

A reference is an entity that is an alias for another object.

A reference is not a variable as a variable is only introduced by the declaration of an object. An object is a region of storage and, in C++, references do not (necessarily) take up any storage.

As objects and references are distinct groups of entities in C++ so the term "reference variable" isn't meaningful.

查看更多
高级女魔头
6楼-- · 2018-12-31 12:56

It's a variable which references another one:

int foo;
int& bar = foo;

bar is now a reference, which is to say that bar holds the location of memory where foo lies.

See here for more information.

查看更多
余生请多指教
7楼-- · 2018-12-31 12:56

A reference variable and a pointer variable are the exact same thing to the machine (the compiler will generate the same machine code).

The most obvious advantages of using a reference variable over a pointer variable in my knowledge:

  1. Easy to understand (no address, de-reference all kinds of headache things)
  2. Saves you a tiny bit of typing, adn thus probably less error-prone.

In the code below, the left side is using a reference variable, and the right side is using a pointer variable. They are the same thing to the machine, but you see the using reference variable saves you a little bit of typing.

Reference variable           Pointer variable
int a = 1;         ~~~~~~    int a = 1;
int &b = a;        ~~~~~~    int *b = &a;
b = 2;             ~~~~~~    *b = 2;
cout << a << '\n'  ~~~~~~    cout << a << '\n'
==============================================
2                  ~~~~~~    2
查看更多
登录 后发表回答