Jump to content
  • Advertisement
Sign in to follow this  
EmrldDrgn

Base Class Pointers, Derived Class operator==

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!