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:34

A reference is an alternative name for an object. A reference variable provides an alias for previously defined variables. A reference declaration consists of a base type an & a reference variable equated to a variable name.

查看更多
几人难应
3楼-- · 2018-12-31 12:36

A reference variable provides an alias (alternative name) for a previously defined variable. For example:

float total=100;
float &sum = total;

It means both total and sum are the same variables.

cout<< total;
cout << sum;

Both are going to give the same value, 100. Here the & operator is not the address operator; float & means a reference to float.

查看更多
美炸的是我
4楼-- · 2018-12-31 12:41

Reference variables allow two variable names to address the same memory location:

int main()
{
    int var1;

    // var2 is a reference variable, holds same value as var1

    int &var2 = var1;
    var1 = 10;

    std::cout << "var1 = " << var1 << std::endl;
    std::cout << "var2 = " << var2 << std::endl;
}

Resource: LINK

查看更多
心情的温度
5楼-- · 2018-12-31 12:42

A Reference variable is an alias for the variable name.

It is different from the pointers in following ways:

  1. You cannot have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage.
  2. Once a reference is initialized to an object, it cannot be changed to point to any another object whereas in case of pointer we can make it point to any other object at any time.
  3. A reference must be initialized the time it is created. Pointers can be made initialized at any time.
查看更多
旧时光的记忆
6楼-- · 2018-12-31 12:46

C++ references allow you to create a second name for the a variable that you can use to read or modify the original data stored in that variable

For More Details Visit:

http://www.cprogramming.com/tutorial/references.html

http://www.tutorialspoint.com/cplusplus/cpp_references.htm

查看更多
泪湿衣
7楼-- · 2018-12-31 12:46

A reference is an alternative label, an alias, for the object it is initialized with. Once a reference is initialized it can not be changed to be an alternative label or alias of some other object. After the initialization the reference or the object variable may be used interchangeably.

A reference has some of the characteristics of a const pointer to an object in that it is initialized when defined. And while what it references or points to can be changed, the reference or the const pointer itself can not be changed. However since a reference is an alternative label or alias it may or may not actually exist as a data object unlike a const pointer which will probably exist unless the compiler can optimize it away. And even if a reference is created as an actual entity by the compiler, that is compiler housekeeping and should be ignored since it officially does not exist much like the man behind the curtain in the Emerald City.

The following code samples gives examples comparing and contrasting reference with pointer and const pointer:

int myInt;           // create a variable of type int, value not initialized
int myInt2 = 3;      // create a second variable of type int with a value of 3
int &rInt = myInt;   // create a reference to the variable of type int, myInt

rInt = 5;            // myInt now has a value of 5, the reference is an alias for myInt
rInt++;              // myInt now has a value of 6, the reference is an alias for myInt
rInt = myInt2;       // myInt now has the same value as myInt2, a value of 3
int *pInt = &rInt;   // pInt points to myInt
(*pInt)++;           // increments myInt
pInt++;              // increments the pointer which formerly pointed to myInt

int &rInt2;          // error C2530: 'rInt2' : references must be initialized
int *pInt2;          // just fine, uninitialized pointer is ok
int * const pInt3;   // error C2734: 'pInt3' : const object must be initialized if not extern
int * const pInt4 = &myInt;  // define and initialize const pointer
pInt4 = &myInt2;     //  error C3892: 'pInt4' : you cannot assign to a variable that is const

There are actually two kinds of references: an lvalue reference and an rvalue reference.

An lvalue reference is the same reference in the C++ language before C++11. An rvalue reference was introduced in C++11 to allow for a reference to a temporary object to assist with doing a move rather than a copy and some other actions where a copy is the wrong approach but a move is the right approach.

For example a comparison of lvalue reference and rvalue reference in the following simple source lines. Because these are int references that means that an assignment of a non-integer value results in the compiler doing a conversion which results in a temporary variable. An rvalue reference can bind to a temporary variable and an lvalue reference can not.

// assign a double to an int causing creation of temporary    
int &rIntd1 = 1.2;   // error C2440: 'initializing' : cannot convert from 'double' to 'int &'
int &&rIntd2 = 1.2;  // warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data

rInt = rIntd2;       // myInt from the code above now has a value of 1, 1.2 was truncated when converting from double to int
查看更多
登录 后发表回答