Export (0) Print
Expand All

Reference Operator: &

Holds the address of an object but behaves syntactically like an object.


type-id & cast-expression

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.

// 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;
}

Community Additions

ADD
Show:
© 2014 Microsoft