Jump to content
  • Advertisement
Sign in to follow this  
jamesleighe

Epiphany (pointer comparison)

This topic is 2615 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

So, pretty much I can sum this up in code.


void FindThingInArray (Thing* thing)
{
for (uint32 i = 0; i < vector.Count (); ++i)
{
if (vector->GetID () == thing->GetID ())
return i;
}
}


Notice in the second one, I'm just comparing pointers!

void FindThingInArray (Thing* thing)
{
for (uint32 i = 0; i < vector.Count (); ++i)
{
if (vector == thing)
return i;
}
}


This should work in all cases (where the pointers point to the same thing) right?
It seems too useful to be true.

My test program seems to work fine but is there some reason I shouldn't do this? (before i convert over loads of code)

Share this post


Link to post
Share on other sites
Advertisement
Yes. C++ <algorithm>'s find :D

And also watch out. Using pointers as object identities may lead to issues down the road (serialization/deserialization, network, ...)

Share this post


Link to post
Share on other sites

but is there some reason I shouldn't do this?


Only if there's a chance you are comparying a *copy* of the object. ie...

[source lang="cpp"]
Thing b = *vector;
return FindThingInArray(&b); //fails in your second example, but succeeds in your first
[/source]

Share this post


Link to post
Share on other sites
Yes this always works. A 'common' issue with this beside the serialization etc. already mentioned is that you cannot replace instances if you use pointers as identifiers (ie. swap one object for another (most often of different derived types)).

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!