Skip to main content
GameDev.net gamedev.net
🔒 Locked

Object ids in C++?

Started by Barius Jun 24, 2007 at 3:37 PM 4 replies 1.3k views
Original Post
Barius
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.
Nypyren
Nypyren
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.
Emmanuel Deloget
Emmanuel Deloget
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 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,
ToohrVyk
ToohrVyk
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.
the_edd
the_edd
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(dynamic_cast(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
MaulingMonkey
MaulingMonkey
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.