可能重复:
What’s the right way to overload operator== for a class hierarchy?相等测试在C派生类++
在C++中,怎样才能派生类覆盖了基类的平等测试以有意义的方式?
例如,假设我有一个基类A.类B和C从A.现在给定的两个指针给两个A的对象派生,我可以测试它们是否相等(包括任何亚类的数据)?
class A {
public: int data;
};
class B : public A {
public: float more_data; bool something_else;
};
class C : public A {
public: double more_data;
};
A* one = new B;
A* two = new B;
A* three = new C;
//How can I test if one, two, or three are equal
//including any derived class data?
有没有干净的做法呢?我最好的选择是什么?
谢谢!
可能的重复:http://stackoverflow.com/questions/1691007/whats-the-right-way-to-overload-operator-for-a-class-hierarchy –
你想比较'T == T ',其中'T'可能是'A','B'或'C'(罚款),或者你想用'C'和'B'将'A'与'B'和'A'进行比较与'C'(可疑)? – sbi
在上面的例子中,我想比较一与二和三。他们都是A指针。 – Imbue