pyo*_*yon 2 c++ const operators equals-operator
我这篇帖子,我看过这个:
class MonitorObjectString: public MonitorObject {
// some other declarations
friend inline bool operator==(/*const*/ MonitorObjectString& lhs,
/*const*/ MonitorObjectString& rhs)
{ return lhs.fVal==rhs.fVal; }
}
Run Code Online (Sandbox Code Playgroud)
在我们继续之前,这是非常重要的:
这是为了以防万一.这也很重要:
/*const*/并重新格式化了代码.现在,我们回到主题:
我想不出一个合理运算符的合理使用,它允许你修改它的by-ref参数.你呢?
他们很可能忘记了const.运算符重载应该始终如一,并且不执行"超出字符"操作.
作为一般规则,相等运算符永远不应修改它正在比较的任何对象.声明const在编译器级别强制执行此操作.但是,它经常被遗漏.在C++中经常忽略"Const正确性".