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?
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!).