Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice : How to get a unique identifier for the object

Tags:

c++

I've got several objects and need to generate a unique identifier for them which will not be changed/repeated during the lifetime of each object.

Basically I want to get/generate a unique id for my objects, smth like this

int id = reinterpret_cast<int>(&obj);

or

int id = (int)&obj;

I understand the codes above are bad ideas, as int might not be large enough to store the address etc.

So whats the best practice to get a unique identifier from the object, which will be a portable solution ?

like image 713
deimus Avatar asked Jan 29 '13 14:01

deimus


People also ask

How do I get a unique identifier?

The simplest way to generate identifiers is by a serial number. A steadily increasing number that is assigned to whatever you need to identify next. This is the approached used in most internal databases as well as some commonly encountered public identifiers.

What is the best identifier to use to uniquely identify a device?

ANDROID_ID is the preferred device identifier. ANDROID_ID is perfectly reliable on versions of Android <=2.1 or >=2.3.

What is used to uniquely identify an object?

A Universal Unique Identifier (UUID) is a 128-bit number used to uniquely identify some object or entity on the Internet. A global unique identifier (GUID) is a number that Microsoft programming generates to create a unique identity for an entity such as a Word document.

What is an example of a unique identifier?

Some agencies give people a 'unique identifier' instead of using their name. Examples are a driver's licence number, a passport number, a student ID number, or an IRD number.


2 Answers

Depending on your "uniqueness"-requirements, there are several options:

  • If unique within one address space ("within one program execution") is OK and your objects stay where they are in memory then pointers are fine. There are pitfalls however: If your objects live in containers, every reallocation may change your objects' identity and if you allow copying of your objects, then objects returned from some function may have been created at the same address.
  • If you need a more global uniqueness, for instance because you are dealing with communicating programs or data that is persistent, use GUIDs/UUIds, such as boost.uuid.
  • You could create unique integers from some static counter, but beware of the pitfalls:
    • Make sure your increments are atomic
    • Protect against copying or create your custom copy constructors, assignment statements.

Personally, my choice has been UUIDs whenever I can afford them, because they provide me some ease of mind, not having to think about all the pitfalls.

like image 83
dhavenith Avatar answered Sep 20 '22 17:09

dhavenith


If the objects need to be uniquely identified, you can generate the unique id in the constructor:

struct Obj
{ 
   int _id;
   Obj() { static int id = 0; _id = id++; }
};

You'll have to decide how you want to handle copies/assignments (same id - the above will work / different id's - you'll need a copy constructor and probably a static class member instead of the static local variable).

like image 28
Luchian Grigore Avatar answered Sep 17 '22 17:09

Luchian Grigore