Comparing objects and inheritance
- by ereOn
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.