分页: 1 / 1

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

发表于 : 2019年01月17日 19:47
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;
}

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

发表于 : 2019年08月25日 17:41
rubyish
3Q~~