Jump to content
  • Advertisement
Sign in to follow this  
Barius

Object ids in C++?

This topic is 4190 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 (in C++) a way to find, for a given reference or pointer to an object, an integer that at least during the lifetime of the object uniquely identifies it? I understand that you can't just use the pointer value (cast to int), because when using multiple inheritance, pointers of different static types to the same object can have different values. I wonder if this is possible at all.

Share this post


Link to post
Share on other sites
Advertisement
As far as I know there's no built-in way to do it. You'll have to create your own. There are tons of different ways to do this.

The most "automatic" might be to make replacements for "new" and "delete" that gives each dynamically allocated object an integer ID along with the "housekeeping" data that gets placed just before the newly allocated memory. Then you could add a getter function that just goes back X bytes before whatever memory address you give it that returns that ID. Totally unsafe, but that's C++ for you.

Share this post


Link to post
Share on other sites
Quote:
Original post by Barius

Is there (in C++) a way to find, for a given reference or pointer to an object, an integer that at least during the lifetime of the object uniquely identifies it? I understand that you can't just use the pointer value (cast to int), because when using multiple inheritance, pointers of different static types to the same object can have different values.

I wonder if this is possible at all.


Isn't that some kind of RTTI scheme? If yes then you can use the standard RTTI stuff - typeid(my_object) - that should help (although you won't get an integer but an instance of the typeinfo class, which is defined in the <typeinfo> header).

Take care that to enable the correct typeid() behavior, you should
1) enable RTTI for your compiler (VC++ disable it by default, as it generates more code and data)
2) read the documentation of typeid() carefully - it's not that easy to understand, especially when it comes to polymorphic types.

Regards,

Share this post


Link to post
Share on other sites
Quote:
Original post by Barius
Is there (in C++) a way to find, for a given reference or pointer to an object, an integer that at least during the lifetime of the object uniquely identifies it? I understand that you can't just use the pointer value (cast to int), because when using multiple inheritance, pointers of different static types to the same object can have different values.


Invasively:
class Identified
{
public:
const int id;
Identified() : id(this) {}
};


However, it would be simpler if we knew what you wanted to use this for.

Share this post


Link to post
Share on other sites
Quote:
Original post by Barius

Is there (in C++) a way to find, for a given reference or pointer to an object, an integer that at least during the lifetime of the object uniquely identifies it? I understand that you can't just use the pointer value (cast to int), because when using multiple inheritance, pointers of different static types to the same object can have different values.


You can cast the pointer to a (possibly cv qualified) void* using dynamic_cast<> which is guaranteed to give you the address of the most derived object i.e.

BOOST_STATIC_ASSERT(sizeof(int_type) >= sizeof(void *));
int_type id = reinterpret_cast<int_type>(dynamic_cast<const volatile void *>(this));

But this kind of thing smells really bad. There's probably a better way, which you should seek out.

Start by asking yourself:
1. why do I need a unique id for every object?
2. why does such an identifier need to be of integral type (or specifically an int)?
3. can I redesign the code to avoid the need for this unique id?

Edd

Share this post


Link to post
Share on other sites
Do you really need object IDs from multiple leafs of an MI tree? Typically not, unless they share a common base -- in which case you're using virtual inheritance anyways.

The usual problems with pointers-as-object-ids are:

1) Specific to the local computer
2) A pain to (un)serialize (might not be loaded into the same memory space on reload).
3) Non contiguous (object-id based tables cannot be simple flat arrays with the id as the index).

The usual solution here is to roll your own.

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!