其参数是非常量引用的运算符 ==
我这篇文章,我看到了这个:
class MonitorObjectString: public MonitorObject {
// some other declarations
friend inline bool operator==(/*const*/ MonitorObjectString& lhs,
/*const*/ MonitorObjectString& rhs)
{ return lhs.fVal==rhs.fVal; }
}
在我们继续之前,这非常重要:
- 我我不质疑任何人的编码能力。
- 我只是想知道为什么有人在比较中需要非常量引用。
- 该问题的发布者 没有编写该代码。
这只是以防万一。 这也很重要:
- 我添加了两个
/*const*/
并重新格式化了代码。
现在,我们回到主题:
我想不出可以让您修改其 by-ref 参数的等号运算符的合理用法。 你?
I this post, I've seen this:
class MonitorObjectString: public MonitorObject {
// some other declarations
friend inline bool operator==(/*const*/ MonitorObjectString& lhs,
/*const*/ MonitorObjectString& rhs)
{ return lhs.fVal==rhs.fVal; }
}
Before we can continue, THIS IS VERY IMPORTANT:
- I am not questioning anyone's ability to code.
- I am just wondering why someone would need non-const references in a comparison.
- The poster of that question did not write that code.
This was just in case. This is important too:
- I added both
/*const*/
s and reformatted the code.
Now, we get back to the topic:
I can't think of a sane use of the equality operator that lets you modify its by-ref arguments. Do you?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也许这些类使用了某种形式的延迟初始化。 当访问数据时,必须进行正确的初始化,并且必须获取数据。 这可能会改变班级成员。
但是,可以形成延迟初始化,以便不需要对类进行修改。 这可以通过使用 Pimpl 惯用法(通过指向私有类的指针)或通过使用可变关键字(不推荐!)。
Perhaps the classes use a form of lazy initialization. When the data is accessed, proper initialization must occur, and the data must be fetched. This may change class members.
However, lazy initialization can be formed so that modification to the class isn't necessary. This can be accomplished by using the Pimpl idiom (by a pointer to a private class) or by using the mutable keyword (not recommended!).
他们很可能忘记了 const。
运算符重载应该表现一致,并且不执行“不符合特性”的操作。
作为一般规则,相等运算符永远不应该修改它正在比较的任何对象。 声明
const
在编译器级别强制执行此操作。 然而,它经常被遗漏。 在 C++ 中,“常量正确性”经常被忽视。Most likely they forgot the
const
.Operator overloads should behave consistently and not perform 'out of character' actions.
As a general rule, an equality operator should never modify any of the objects it is comparing. Declaring
const
enforces this at the compiler level. However, it is often left out. "Const correctness" is very often overlooked in C++.你提出的问题还有一个更令人困惑的问题。
如果我有两个已经是 const 的
MonitorObjectString
,则无法使用此相等函数。There's a more baffling problem with the issue you bring up.
If I have two
MonitorObjectString
s that are already const, I can't use this equality function.在这种情况下,显然不需要非常量参数,并且像您一样,我认为也没有任何一般情况。
然而,毫无疑问,常量正确性问题可能会从代码的较低级别向上推进,如果您无法从低层纠正它们,那么您可能必须在更高层解决它们。 也许这就是这里发生过的事情?
There's clearly no requirement for non-const args in this case and, like you, I wouldn't think there's any general case for it either.
However, it's certainly the case that const-correctness problems can push their way up from lower levels of the code, and if you can't correct them low-down, then you might have to work around them higher up. Perhaps that's what happened here at some point?
如果因为修改操作数而无法使用 const,那么您就严重滥用了运算符重载。
如果由于实现调用非 const 函数而无法使用 const,那么您确实应该清理它们,或者至少提供 const 替代方案。
如果您正在调用代码,则无法更改不使用 const 的代码,无论如何我都会使用 const,在最深的可用点使用 const_cast 并对其进行注释。
正如 Shmoopty 指出的那样,该运算符的用处远没有应有的那么大,因为它不能用于 const 对象,即使其中只有一个是 const。 不支持“a == 5”的数字相等运算符将在很大程度上违反最小惊讶定律。
If you can't use const because you're modifying operands, you're badly misusing operator overloading.
If you can't use const because the implementation calls non-const functions, you really should clean those up, or at least provide const alternatives.
If you're calling into code you can't change which doesn't use const, I'd use the const anyway, use const_cast at the deepest available point, and comment it.
As Shmoopty pointed out, the operator is a lot less useful than it should be since it can't be used on const objects, even if only one of them is const. A numeric equality operator that wouldn't support "a == 5" would violate the Law of Least Astonishment in a big way.