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;
}
oggmonster
  • 4,672
  • 10
  • 51
  • 73
  • How on Earth do you expect to compare objects of different types? – Mark B Oct 04 '11 at 15:36
  • 1
    The problem of slicing is covered in [this question](http://stackoverflow.com/questions/4403726/learning-c-polymorphism-and-slicing). – Björn Pollex Oct 04 '11 at 15:40
  • Mind you that next to the slicing issue, comparison and polymorphism isn't that straightforward, since polymorphism is mostly about letting objects do something. There are solutions with dynamic_cast and with double dispatch, but it is not straightforward. – stefaanv Oct 04 '11 at 15:54
  • @MarkB I guess I phrased that wrong. What I want to be able to do is compare types that have the same base type. So if I compare type B to type C, the type is compared with the mType member. If both mTypes are the same (ie they are both the same derived type) then all of the members should be compared. – oggmonster Oct 04 '11 at 19:09

1 Answers1

5
  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.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533