[C++]左值引用与右值引用

Cplusplus
回复
头像
vicyang
版主
版主
帖子: 56
注册时间: 2016年07月21日 20:35
联系:

[C++]左值引用与右值引用

帖子 vicyang »

#include <iostream>
#include <string>
using namespace std;
void test1(string&& a, string&& b)
{
cout << "A:" << a << ", B:" << b <<endl;
}

void test2(const string& a, const string& b)
{
cout << "A:" << a << ", B:" << b <<endl;
}

void test3(string& a, string& b)
{
cout << "A:" << a << ", B:" << b <<endl;
}

int main(int argc, char *argv[] )
{
test1( string("abc"), string("123") );
test2( string("abc"), string("123") );
// test3( string("abc"), string("123") ); //error: cannot bind non-const lvalue reference of type
// &ref 是左值引用操作符,字面值常量属于右值,C++规定,右值不能传递给左值引用。
return 0;
}
头像
rubyish
渐入佳境
渐入佳境
帖子: 52
注册时间: 2018年04月23日 09:58
联系:

Re: [C++]左值引用与右值引用

帖子 rubyish »

3Q~~
$_
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 2 访客