C++按值和按引用比较运算符的多态性

发布于 2024-12-07 18:07:27 字数 1182 浏览 0 评论 0原文

我想比较从相同基类型派生但不是相同派生类的两个对象。因此,我将 == 运算符设为虚拟并在派生类中覆盖它。

当我将所有对象存储在基本类型的数组中时,不会调用派生实现,它直接进入基本实现。但是,当数组是指向基类的指针类型并且取消引用元素时,它确实可以工作。

有人可以解释一下为什么会出现这种行为吗?这让我很困惑;-)

enum eType {
  BASE_TYPE,
  DERIVED_TYPE
};

class A {
  eType mType;
public:
  A() : mType(BASE_TYPE) {}
  A(eType Type) : mType(Type) {}
  virtual bool operator == (A &Other) {
    return mType == Other.mType;
  }
};

class B : public A {
  int bVal;
public:
  B(int Val) : A(DERIVED_TYPE), bVal(Val) {}
  virtual bool operator == (A &Other) {
    if(!(A::operator ==(Other))) {
      return false;
    }
    B* myB = (B*)&Other;
    return bVal == myB->bVal;
  }
};

int main(int argc, char *argv[])
{
  A a1, a2;
  B b1(0);
  B b2(1);

  bool result = false;

  // Calls implementation in A
  result = (a1 == a2);
  // Calls implementation in B
  result = (b1 == b2);

  A aArray[2];
  aArray[0] = b1;
  aArray[1] = b2;

  // Calls implementation in A!
  result = (aArray[0] == aArray[1]);

  A *aRefArray[2];
  aRefArray[0] = &b1;
  aRefArray[1] = &b2;

  // Calls implementation in B
  result = ((*aRefArray[0]) == (*aRefArray[1]));

  return 0;
}

I want to compare two objects that are derived from the same base type but are not the same derived class. So I made the == operator virtual and over-ride it in derived classes.

When I store all of my objects in an array of the base type, the derived implementation is not called, it goes straight to the base implementation. It does however work when the array is of type pointer to base class, and a de-reference the elements.

Could some-one please explain why this behaviour occurs? It baffles me ;-)

enum eType {
  BASE_TYPE,
  DERIVED_TYPE
};

class A {
  eType mType;
public:
  A() : mType(BASE_TYPE) {}
  A(eType Type) : mType(Type) {}
  virtual bool operator == (A &Other) {
    return mType == Other.mType;
  }
};

class B : public A {
  int bVal;
public:
  B(int Val) : A(DERIVED_TYPE), bVal(Val) {}
  virtual bool operator == (A &Other) {
    if(!(A::operator ==(Other))) {
      return false;
    }
    B* myB = (B*)&Other;
    return bVal == myB->bVal;
  }
};

int main(int argc, char *argv[])
{
  A a1, a2;
  B b1(0);
  B b2(1);

  bool result = false;

  // Calls implementation in A
  result = (a1 == a2);
  // Calls implementation in B
  result = (b1 == b2);

  A aArray[2];
  aArray[0] = b1;
  aArray[1] = b2;

  // Calls implementation in A!
  result = (aArray[0] == aArray[1]);

  A *aRefArray[2];
  aRefArray[0] = &b1;
  aRefArray[1] = &b2;

  // Calls implementation in B
  result = ((*aRefArray[0]) == (*aRefArray[1]));

  return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光倒影 2024-12-14 18:07:27
  A aArray[2];
  aArray[0] = b1;
  aArray[1] = b2;

当您这样做时,对象切片需要放置并且对象的派生部分刚刚被剥离。现在存储在数组中的对象现在仅表现为基类的对象。当然,操作符函数的基类版本会被调用。


  A *aRefArray[2];
  aRefArray[0] = &b1;
  aRefArray[1] = &b2;

适当地,保留派生类对象的类型,因为存储在数组中的只是指向实际对象的指针,而不是对象本身。
由于保留了对象的类型,因此在这种情况下将调用运算符函数的派生类版本。

  A aArray[2];
  aArray[0] = b1;
  aArray[1] = b2;

When you do that Object Slicing takes place and the derived part of the object just gets stripped off. The objects now stored inside the array now merely behave as objects of the Base class. And naturally, the Base class version of the operator function gets called.


  A *aRefArray[2];
  aRefArray[0] = &b1;
  aRefArray[1] = &b2;

Appropriately, preserves the type of the derived class objects, because what gets stored in the array is just pointer to the actual object not the object itself.
Since the type of the object is preserved the derived class version of the operator functions gets called in this case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文