开发者

C++ Polymorphism of Comparison Operator by Value and by Reference

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;

  // 开发者_如何学PythonCalls 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;
}


  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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜