How to avoid forgetting to define operator== in subclasses?
I have a base class and define a operator== on it. And B is a subclass of
A and I forget to define operator== on B. Then A::operator== is used on
comparing B and usually this gives an unexpected results. Any good method
to avoid such "forget"? I add an example to clarify my question.
class A
{
public:
bool operator==(const A& rhs) const
{
return i == rhs.i;
}
int i
};
class B : public A
{
public:
int j;
}
B b1, b2;
b1.i = 1; b1.j = 2;
b2.i = 1; b1.j = 3;
bool b = (b1 == b2); // will be true
No comments:
Post a Comment