Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Holds the address of an object but behaves syntactically like an object.
type-id & cast-expression
Remarks
A reference declaration consists of an (optional) list of specifiers followed by a reference declarator. A reference must be initialized and cannot be changed.
Any object whose address can be converted to a given pointer type can also be converted to the analogous reference type. For example, any object whose address can be converted to type char * can also be converted to type char &. No constructors or class conversion functions are called to make a conversion to a reference type.
Do not confuse reference declarations with use of the address-of operator. When **& **identifier is preceded by a type, such as int or char, then identifier is declared as a reference to the type. When **& **identifier is not preceded by a type, the usage is that of the address-of operator.
Example
// expre_ReferenceOperator.cpp
// compile with: /EHsc
// Demonstrate reference operator
#include <iostream>
using namespace std;
struct Person {
char *Name;
short Age;
};
int main() {
Person Friend; // Declare the object.
Person& rFriend = Friend; // Declare the reference.
Friend.Name = "Bill";
rFriend.Age = 40;
cout << rFriend.Name << " is " << Friend.Age << endl;
}
See Also
Reference
C++ Operators
Operator Precedence and Associativity
References (C++)
Reference-Type Function Arguments
Reference-Type Function Returns
References to Pointers