求 C++ 中双地址运算符 (&&) 是什么?
&& 是 C++11 标准中定义的一个新的引用运算符。int&& a 表示“a”是一个右值引用。&& 通常用于声明函数的一个参数。它只采用一个右值表达式。
简单来说,右值是没有内存地址的值。例如,数字 6 和字符 'v' 都属于右值。int a,a 是左值,但是 (a+2) 是右值。
示例
void foo(int&& a)
{
//Some magical code...
}
int main()
{
int b;
foo(b); //Error. An rValue reference cannot be pointed to a lValue.
foo(5); //Compiles with no error.
foo(b+3); //Compiles with no error.
int&& c = b; //Error. An rValue reference cannot be pointed to a lValue.
int&& d = 5; //Compiles with no error.
}可以在 http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx 阅读更多有关右值和该运算符的信息。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP