全局类与继承的比较

Global class comparison with inheritance

本文关键字:比较 继承 全局      更新时间:2023-10-16

我正在为一个大型框架编写带有operator==的全局类比较函数,其中类倾向于继承多个类或具有深度继承(类A继承自BB继承自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

如果你对这个结果感到满意,你可以坚持你当前的设计。然而,我认为这是一个不适当的结果。

我的建议:

  1. 使Foo成为纯粹的虚拟类,以避免这种情况。
  2. 使operator=()成为纯虚拟成员函数Foo。提供派生类实现可以利用Foo实现。
  3. 实现派生类的函数。使用 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