# Base Class Pointers, Derived Class operator==

This topic is 3716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is there a way to cause a base class pointer to use the operator== of the derived class it's pointing to? I tried to do something like this:
class ABC
{
...
public:
bool operator==(ABC& rv) = 0;
...
}
and then redefine it for each derived class, but that requires that I use an ABC& as the rvalue, which means I can't compare data which isn't in class ABC, but is instead added in the derived classes. In a perfect world, the code should be able to automagically tell that two objects of two different classes are not equal, but I'm not sure how to make it do so. What I'm trying to do is be able to say, in a Tile*[][] which represents my map:
if(map[x][y] == map[z][w])
//do something
but I have some derived tile types (Animated, Static, Harmful, Healing). In case that helps you suggest a better way to fix the whole mess. Thanks!

##### Share on other sites
This is not equality relation. It's is-a, something which requires RTTI, and is not exactly popular in C++.

For this purpose, defining a bool equals(BaseObject *) would be best.

In body, you can then use dynamic_cast to test for proper types.

##### Share on other sites
if you want to guarantee that the two types are _absolutely_ the same you can do:

if(typeid(map[x][y])==typeid(map[z][w]) and map[x][y] == map[z][w]){  //do something}

but that means that the two types must be _exactly_ the same.

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
13
5. 5

• 9
• 14
• 10
• 9
• 17
• ### Forum Statistics

• Total Topics
632601
• Total Posts
3007359

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!