I got a class that when instantiated needs to obtain a few unique ids to work. Initially I thought using an static function that assigns and increments. I don't need them to be consecutive, only unique.
class A {
int id_1;
int id_2;
int id_3;
public:
static int last_id=0;
static int get_id(){ return A::last_id++; }
...
A(){ id_1 = A::get_id(); id_2 = A::get_id(); id_3 = A::get_id(); }
};
Now, I' thinking in going multithreading. I think the static function will be a bottleneck, since I'm constructing a few hundred thousand instances of these objects at the start. I don't destroy any instance until the end of the program, so after initialization they are fixed. Anyway they are not computed at compile time because the quantity depends of command-line arguments.
An alternative I was thinking of was using memory addresses, they are unique in a single computer at least.
Something like:
class A {
int* id_1;
int* id_2;
int* id_3;
public:
static int last_id=0;
static int get_id(){ return A::last_id++; }
...
A(){ id_1 = new int(0); id_2 = new int(0); id_3 = new int(0); }
~A() { delete id_1; delete id_2; delete id_3(); }
};
Then I would read the identifiers as the address of the pointers.
Question: Does this make any sense to use pointers like this?