Comparing objects and inheritance

Posted by ereOn on Stack Overflow See other posts from Stack Overflow or by ereOn
Published on 2010-05-13T08:49:51Z Indexed on 2010/05/13 8:54 UTC
Read the original article Hit count: 197

Hi,

In my program I have the following class hierarchy:

class Base // Base is an abstract class
{
};

class A : public Base
{
};

class B : public Base
{
};

I would like to do the following:

foo(const Base& one, const Base& two)
{
  if (one == two)
  {
    // Do something
  } else
  {
    // Do something else
  }
}

I have issues regarding the operator==() here. Of course comparing an instance A and an instance of B makes no sense but comparing two instances of Base should be possible. (You can't compare a Dog and a Cat however you can compare two Animals)

I would like the following results:

A == B => false

A == A => true or false, depending on the effective value of the two instances

B == B => true or false, depending on the effective value of the two instances

My question is: is this a good design/idea ? Is this even possible ? What functions should I write/overload ?

My apologies if the question is obviously stupid or easy, I have some serious fever right now and my thinking abilities are somewhat limited :/

Thank you.

© Stack Overflow or respective owner

Related posts about c++

Related posts about inheritance