Greetings all,
Is it possible to craft a future-safe comparison operator (==) in C++?
The problem I faced was that we have a class with multiple members. We have a comparison operator to validate if instance-1 of the object has the same values as instance-2.
i.e. we can do
class blarg {
.....
};
.....
blarg b1(..initializers...);
blarg b2 = b1;
if (b1 == b2) {
... then do something ....
}
However, I had a co-worker that added a new member to the class, but failed to update the comparison operator. This lead to problems that took a while for us to figure out.
Is there a coding practice, I mean other than code review (that failed for us), or coding method, design, template, magic beans, whatever that could help avoid situations like this?
My first reaction was to use the memcmp
command. However, after reading the stack overflow entry for "Comparing structures in C vs C++" I see that this can be problematic due to C++ classes having more than just the member data inside.
How do others deal with this?
Thank you in advance for your help.