考虑如下代码
class Foo
{
public:
int x, y;
};
Foo operator*(const Foo &lhs, const Foo &rhs)
{
Foo ret;
return ret;
}
int main()
{
Foo a, b, c;
(a * b) = c;
return 0;
}
operator*(a, b)返回的应该是一个右值,为什么可以被赋值呢??编译器没有提示错误。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你的Code是有问题的,在我这儿.
你的代码是例子代码吧? 你的重载运算符是错误的. 格式都写错了吧? 重载不应该是如下吗?
不太明白您的意思.
我记得右值也可以是被赋值的.如果函数返回的右值是一个引用呢?
就像下面这段代码
不能把“左值”、“右值”简单理解为出现在等号两边的位置。尤其是一个类对象出现在等号左边时,对象赋值实际是通过调用函数
operator=
完成的:(a * b) = c
==>(a*b).operator=(c)