自定义==运算符,哪一边重要吗?
custom == operator, does it matter which side?
JSON Spirit有一个方便operator==
template< class Config >
bool Value_impl< Config >::operator==( const Value_impl& lhs ) const
{
if( this == &lhs ) return true;
if( type() != lhs.type() ) return false;
return v_ == lhs.v_;
}
变量lhs
看起来像许多其他示例中熟悉的"左侧",这意味着如果为该运算符分配的内容不在左侧,这将无法按预期工作。
这是对的吗? 如果是这样,为什么?
无论哪种情况,请引用标准。
b = x == y;
翻译为b = x.operator==( y );
因此必须为x
定义一个operator==()
,该它接受任何类型的参数y
。
class Y
{
public:
};
class X
{
public:
bool operator==( Y rhs )
{
return false;
}
};
void tester()
{
X x;
Y y;
bool b = x == y; // works
b = y == x; // error
}
相关文章:
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何正确实现和访问运算符的各种自定义枚举器
- 自定义先决条件对移动分配运算符有效吗
- C++ - 没有自定义交换功能的移动分配运算符?
- C++ 中完全自定义的流运算符
- 如何在自定义对象的<<运算符中添加自定义前缀
- C++ 通过自定义赋值运算符隐式转换函数参数
- 在 C++ 的自定义运算符中删除与删除[](不同于常见的删除与删除[]问题)
- 自定义运算符重载C++,无开销
- C++自定义删除运算符不能正常工作?
- 如何使用运算符>>在自定义字符串中输入多个单词?
- 如何处理自定义输出运算符中的 iomanips?
- 在priority_queue的自定义类中重载比较运算符
- 重载C++中的[]运算符以支持自定义设置函数
- 运算符的歧义错误<<自定义 std::ostream 子类中的重载
- 如何使用C++将MXNET自定义运算符构建到单独的库/包中?
- 自定义容器中的括号运算符中的恒常性
- 重写自定义数组类的运算符/开始/结束
- boost::asio::async_read_until 具有自定义匹配条件运算符重载混淆
- 自定义类型转换运算符在转发引用上调用时不起作用(当对象按值传递时有效)