What is a reference variable in C++?
C++中引用变量的简短定义是什么?
引用是一个实体,它是另一个对象的别名。
引用不是变量,因为变量只由对象的声明引入。对象是存储区域,在C++中,引用不必(占用)占用任何存储。
由于对象和引用是C++中不同实体的组,因此"引用变量"一词没有意义。
引用变量为以前定义的变量提供别名(可选名称)。例如:
1 2 | float total=100; float &sum = total; |
这意味着
1 2 | cout<< total; cout << sum; |
两者的价值相同,
引用变量是对象的别名(备用名称)。[来自C++的FAQ ]。
维基百科文章的第一段可以很容易地作为一个简短的定义:
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.
引用同一篇文章:
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.
进一步阅读:
- 维基百科:参考C++
- C+:C++参考文献
- 堆栈溢出:C++中指针变量与引用变量的区别
引用变量是变量名的别名。
它与指针有以下不同:
C++引用允许您为变量创建第二个名称,可以用来读取或修改存储在该变量中的原始数据。
有关详细信息,请访问:
http://www.cprogramming.com/tutorial/references.html
http://www.tutorialspoint.com/cplusplus/cpp_references.htm
它是一个引用另一个变量的变量:
1 2 | int foo; int& bar = foo; |
有关详细信息,请参阅此处。
引用变量允许两个变量名处理相同的内存位置:
1 2 3 4 5 6 7 8 9 10 11 12 | 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; } |
资源:链接
引用是初始化对象的替代标签、别名。一旦一个引用被初始化,它就不能被更改为另一个对象的标签或别名。初始化之后,引用或对象变量可以互换使用。
引用具有指向对象的常量指针的某些特性,因为它在定义时被初始化。虽然它引用的或指向的内容可以更改,但引用或常量指针本身不能更改。但是,由于引用是另一个标签或别名,因此它可能作为数据对象存在,也可能不作为数据对象存在,这与常量指针不同,常量指针可能存在,除非编译器可以对其进行优化。即使一个引用是由编译器作为一个实际的实体创建的,那就是编译器内务管理,应该被忽略,因为它正式地并不存在,就像翡翠城窗帘后面的人一样。
以下代码示例给出了将引用与指针和常量指针进行比较和对比的示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 |
实际上有两种引用:
在C++ 11之前,EDCOX1引用0引用是C++语言中的相同引用。在C++ 11中引入了一个EDCOX1×1引用,以允许引用一个临时对象来帮助执行一个动作,而不是一个拷贝和一些其他操作,其中一个拷贝是错误的方法,但是移动是正确的方法。
例如,在下面的简单源代码行中比较左值引用和右值引用。因为这些是
1 2 3 4 5 | // 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 |
第79页~第80页+引物第五版。
Object: A region of memory that has a type
Variable: Named object or reference
Reference: An alias for another object.
引用是对象的替代名称。引用变量为以前定义的变量提供别名。引用声明由基类型和等同于变量名的引用变量组成。
参考变量(
例如,假设两个室友共用一个房间。一个朋友叫
引用变量和指针变量与机器完全相同(编译器将生成相同的机器代码)。
在我的知识中,使用参考变量比使用指针变量最明显的优点是:
在下面的代码中,左侧使用引用变量,右侧使用指针变量。它们与机器是相同的,但是您可以看到using引用变量为您节省了一些输入。
1 2 3 4 5 6 7 8 9 | Reference variable Pointer variable int a = 1; ~~~~~~ int a = 1; int &b = a; ~~~~~~ int *b = &a; b = 2; ~~~~~~ *b = 2; cout << a << ' ' ~~~~~~ cout << a << ' ' ============================================== 2 ~~~~~~ 2 |