I'm writing a function (or operator==()
in C++), comparing two objects. The code looks like this:
bool operator==(const Obj& copy1, const Obj& copy2)
{
if(copy1.a!=copy2.a)
return false;
if(copy1.b!=copy2.b)
return false;
if(copy1.c!=copy2.c)
return false;
return true;
}
There may be many more data members to compare, and not all members present in Obj
are to be compared, so using memcmp()
isn't an option. There also might be more complicated tests than just equality of the members. Now there's another way:
bool operator==(const Obj& copy1, const Obj& copy2)
{
return (copy1.a==copy2.a &&
copy1.b==copy2.b &&
copy1.c==copy2.c);
}
The first way seems more verbose, but also easier to modify, while the second seems somewhat fragile (can't explain why, just gut feeling), but looks a lot simpler.
What is a better way? Is there any real difference in readability or maintainability of these approaches?