Skip to content

Latest commit

 

History

History
79 lines (64 loc) · 2.79 KB

File metadata and controls

79 lines (64 loc) · 2.79 KB
Error in user YAML: (<unknown>): did not find expected ',' or ']' while parsing a flow sequence at line 11 column 22
---
title: "Lvalue Reference Declarator: &amp; | Microsoft Docs"
ms.custom: ""
ms.date: "11/04/2016"
ms.reviewer: ""
ms.suite: ""
ms.technology: ["cpp-language"]
ms.tgt_pltfrm: ""
ms.topic: "language-reference"
f1_keywords: ["&"]
dev_langs: ["C++"]
helpviewer_keywords: ["[""reference operator [C++]"", ""& operator [C++], reference operator""]"]
ms.assetid: edf0513d-3dcc-4663-b276-1269795dda51
caps.latest.revision: 14
author: "mikeblome"
ms.author: "mblome"
manager: "ghogen"
translation.priority.ht: ["cs-cz", "de-de", "es-es", "fr-fr", "it-it", "ja-jp", "ko-kr", "pl-pl", "pt-br", "ru-ru", "tr-tr", "zh-cn", "zh-tw"]
---

Lvalue Reference Declarator: &

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

Syntax

  
type-id & cast-expression  

Remarks

You can think of an lvalue reference as another name for an object. An lvalue 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 similar reference type. For example, any object whose address can be converted to type char * can also be converted to type char &.

Do not confuse reference declarations with use of the address-of operator. When the &identifier is preceded by a type, such as int or char, 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

The following example demonstrates the reference declarator by declaring a Person object and a reference to that object. Because rFriend is a reference to myFriend, updating either variable changes the same object.

// reference_declarator.cpp  
// compile with: /EHsc  
// Demonstrates the reference declarator.  
#include <iostream>  
using namespace std;  
  
struct Person  
{  
    char* Name;  
    short Age;  
};  
  
int main()  
{  
   // Declare a Person object.  
   Person myFriend;  
  
   // Declare a reference to the Person object.  
   Person& rFriend = myFriend;  
  
   // Set the fields of the Person object.  
   // Updating either variable changes the same object.  
   myFriend.Name = "Bill";  
   rFriend.Age = 40;  
  
   // Print the fields of the Person object to the console.  
   cout << rFriend.Name << " is " << myFriend.Age << endl;  
}  
Bill is 40  

See Also

References
Reference-Type Function Arguments
Reference-Type Function Returns
References to Pointers