全局类与继承的比较
Global class comparison with inheritance
我正在为一个大型框架编写带有operator==
的全局类比较函数,其中类倾向于继承多个类或具有深度继承(类A
继承自B
,B
继承自C
等)。为了使事情易于管理,我想我会有一个用于基类的比较函数,然后从基类继承的类除了检查自己的成员外,还会使用该函数
在谷歌上搜索,我找到了用于比较类的示例代码,但没有涉及继承的示例。下面我为基类Foo
制作了一个简单的示例,Bar
继承自:
#include <iostream>
class Foo
{
public:
int m_a;
Foo(int i) : m_a(i) {}
};
inline static bool operator==(const Foo& l, const Foo& r)
{
return l.m_a == r.m_a;
}
static void coutResult(const Foo& l, const Foo&r)
{
std::cout << "l.m_a == " << l.m_a << ", "
<< "r.m_a == " << r.m_a << ", "
<< (l == r ? "true" : "false") << std::endl;
}
class Bar :
public Foo
{
public:
int m_b;
Bar(int i, int j) : Foo(i), m_b(j) {}
};
inline static bool operator==(const Bar& l, const Bar& r)
{
return ((Foo)l) == ((Foo)r) &&
l.m_b == r.m_b;
}
static void coutResult(const Bar& l, const Bar& r)
{
std::cout << "l.m_a == " << l.m_a << ", "
<< "l.m_b == " << l.m_b << ", "
<< "r.m_a == " << r.m_a << ", "
<< "r.m_b == " << r.m_b << ", "
<< (l == r ? "true" : "false") << std::endl;
}
int main(int argc, char** argv) {
Foo a(1);
Foo b(1);
Foo c(2);
coutResult(a, b);
coutResult(a, c);
coutResult(a, c);
Bar d(1, 2);
Bar e(1, 2);
Bar f(1, 3);
Bar g(2, 2);
coutResult(d, e);
coutResult(d, f);
coutResult(d, g);
coutResult(e, f);
coutResult(f, g);
coutResult(f, g);
return 0;
}
它似乎工作得很好,但我想知道是否有一种"标准"方法来解决这个问题或更好的解决方案。我看到此解决方案存在两个问题:
每次开发人员将成员添加到某个类时,他们都必须知道更新相应的比较函数,但我看不出如何避免这种情况
没有成员可以成为私有的,考虑到框架很大,这是一个问题。我所知道的唯一解决方案是为每个私人成员制作一个getter
您的设计有可能产生意想不到的结果。
如果您的main
是:
int main(int argc, char** argv)
{
Foo a(1);
Bar d(1, 2);
coutResult(a, d);
return 0;
}
您最终会将Foo
对象与Bar
对象进行比较,输出将是:
l.m_a == 1, r.m_a == 1, true
如果你对这个结果感到满意,你可以坚持你当前的设计。然而,我认为这是一个不适当的结果。
我的建议:
- 使
Foo
成为纯粹的虚拟类,以避免这种情况。 - 使
operator=()
成为纯虚拟成员函数Foo
。提供派生类实现可以利用Foo
实现。 - 实现派生类的函数。使用
dynamic_cast
确保将一个Bar
与另一个Bar
进行比较,而不是与另一个子类型的Foo
进行比较Bar
。
这是一个演示这些想法的程序。
#include <iostream>
class Foo
{
public:
int m_a;
Foo(int i) : m_a(i) {}
virtual bool operator==(const Foo& r) const = 0;
};
bool Foo::operator==(const Foo& r) const
{
return (this->m_a == r.m_a);
}
static void coutResult(const Foo& l, const Foo&r)
{
std::cout << std::boolalpha << (l == r) << std::endl;
}
class Bar : public Foo
{
public:
int m_b;
Bar(int i, int j) : Foo(i), m_b(j) {}
virtual bool operator==(const Foo& r) const
{
Bar const* barPtr = dynamic_cast<Bar const*>(&r);
if ( barPtr == nullptr )
{
return false;
}
if ( !Foo::operator==(r) )
{
return false;
}
return (this->m_b == barPtr->m_b);
}
};
class Baz : public Foo
{
public:
double m_c;
Baz(int i, double c) : Foo(i), m_c(c) {}
virtual bool operator==(const Foo& r) const
{
Baz const* bazPtr = dynamic_cast<Baz const*>(&r);
if ( bazPtr == nullptr )
{
return false;
}
if ( !Foo::operator==(r) )
{
return false;
}
return (this->m_c == bazPtr->m_c);
}
};
int main(int argc, char** argv)
{
Bar bar1(1, 2);
Bar bar2(1, 2);
Bar bar3(2, 2);
Baz baz1(1, 10.8);
Baz baz2(1, 10.8);
coutResult(bar1, bar2);
coutResult(bar1, bar3);
coutResult(bar1, baz1);
coutResult(baz1, baz2);
return 0;
}
输出:
true
false
false
true
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 继承函数的重载解析
- 为什么比较运算符如此快速
- 继承期间显示未知行为的子类
- 我可以使用 g++ 进行三种比较 (<=>) 吗?
- 头文件-继承c++
- 为什么在保护模式下继承升级不起作用
- 比较字符数组
- 将模板化的类型与C++中的某些类/类型进行比较
- 通过继承类使用来自不同命名空间的运算符
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- C++自定义比较函数
- 继承类中的比较运算符
- 继承比较运算符,但不能将派生类彼此进行比较
- 比较从父类继承但存储在父类的向量中的类的对象类型
- 在 c++ 中,通常比较继承层次结构中的对象
- 多重继承指针比较
- 继承中的对象比较
- 全局类与继承的比较
- 比较指针.多重继承问题