这是书中的一道题目,为什么就combine2不能转换,而combine1和3就没问题?
#include <string>
using namespace std;
class Sales_data {
public:
Sales_data() = default;
Sales_data(const string& str) :data(str) {}
Sales_data &combine1(Sales_data a) {}
Sales_data &combine2(Sales_data& a) {}
Sales_data &combine3(const Sales_data& a)const {}
private:
string data;
};
int main()
{
string str = "hello";
Sales_data item("9-999-99999-9");
item.combine1(str);
item.combine2(str);//error:无法将参数 1 从“std::string”转换为“Sales_data &”
item.combine3(str);
}
The temporary object generated during implicit conversion is an rvalue, while the
reference can only be bound to an lvalue, and the const reference can also be bound to an rvalue
Want to take another look at "Quickly Understand Lvalues and Rvalues in C/C++"
http://segmentfault.com/a/1190000003793498