What would be a brief definition of a reference variable in C++?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
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.
A reference variable provides an alias (alternative name) for a previously defined variable. For example:
It means both
total
andsum
are the same variables.Both are going to give the same value,
100
. Here the&
operator is not the address operator;float &
means a reference to float.Reference variables allow two variable names to address the same memory location:
Resource: LINK
A Reference variable is an alias for the variable name.
It is different from the pointers in following ways:
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
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:
There are actually two kinds of references: an
lvalue
reference and anrvalue
reference.An
lvalue
reference is the same reference in the C++ language before C++11. Anrvalue
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. Anrvalue
reference can bind to a temporary variable and anlvalue
reference can not.