引用 (C++)References (C++)

与指针相似的是,引用将存储位于内存中其他位置的对象的地址。A reference, like a pointer, stores the address of an object that is located elsewhere in memory. 与指针不同的是,初始化之后的引用无法引用不同的对象或设置为 null。Unlike a pointer, a reference after it is initialized cannot be made to refer to a different object or set to null. 有两种类型的引用: 左值引用,后者是指已命名的变量和右值引用后者是指临时对象There are two kinds of references: lvalue references which refer to a named variable and rvalue references which refer to a temporary object. & 运算符表示左值引用和 & & 运算符表示右值引用或根据上下文的通用引用 (右值或左值)。The & operator signifies an lvalue reference and the && operator signifies either an rvalue reference, or a universal reference (either rvalue or lvalue) depending on the context.

可以通过以下语法声明引用:References may be declared using the following syntax:

[storage-class-specifiers] [cv-qualifiers] type-specifiers [ms-modifier] declarator [= expression];[storage-class-specifiers] [cv-qualifiers] type-specifiers [ms-modifier] declarator [= expression];

可以使用指定引用的任何有效声明符。Any valid declarator specifying a reference may be used. 除非引用是对函数或数组类型的引用,否则应用以下简化语法:Unless the reference is a reference to function or array type, the following simplified syntax applies:

[存储类说明符] [ cv 限定符]类型说明符 [ &&&] [ cv 限定符]标识符 [ = 表达式];[storage-class-specifiers] [cv-qualifiers] type-specifiers [& or &&] [cv-qualifiers] identifier [= expression];

使用以下序列声明引用:References are declared using the following sequence:

  1. 声明说明符:The declaration specifiers:

    • 可选存储类说明符。An optional storage class specifier.

    • 可选const和/或易失性限定符。Optional const and/or volatile qualifiers.

    • 类型说明符:类型的名称。The type specifier: the name of a type.

  2. 声明符:The declarator:

    • 可选的 Microsoft 专用修饰符。An optional Microsoft specific modifier. 有关详细信息,请参阅Microsoft 专用的修饰符For more information, see Microsoft-Specific Modifiers.

    • & 运算符或&& 运算符。The & operator or && operator.

    • 可选const和/或易失性限定符。Optional const and/or volatile qualifers.

    • 标识符。The identifier.

  3. 可选初始值设定项。An optional initializer.

指向数组和函数的更复杂声明符形式也适用于对数组和函数的引用。The more complex declarator forms for pointers to arrays and functions also apply to references to arrays and functions. 有关详细信息,请参阅指针For more information, see pointers.

多个声明符和初始值设定项可能出现在一个声明说明符后面的逗号分隔的列表中。Multiple declarators and initializers may appear in a comma-separated list following a single declaration specifier. 例如:For example:

int &i;
int &i, &j;

引用、指针和对象可以一起声明:References, pointers and objects may be declared together:

int &ref, *ptr, k;

引用保留对象的地址,但语法行为与对象一样。A reference holds the address of an object, but behaves syntactically like an object.

在下面的程序中,请注意对象的名称 s 和对象的引用 SRef 可在程序中以相同的方式使用:In the following program, notice that the name of the object, s, and the reference to the object, SRef, can be used identically in programs:

示例Example

// references.cpp
#include <stdio.h>
struct S {
   short i;
};

int main() {
   S  s;   // Declare the object.
   S& SRef = s;   // Declare the reference.
   s.i = 3;

   printf_s("%d\n", s.i);
   printf_s("%d\n", SRef.i);

   SRef.i = 4;
   printf_s("%d\n", s.i);
   printf_s("%d\n", SRef.i);
}
3
3
4
4

请参阅See also

引用类型函数自变量Reference-Type Function Arguments
引用类型函数返回Reference-Type Function Returns
对指针的引用References to Pointers