I have a bunch of C++ classes.
I want each class to have something like:
static int unique_id;
All instances of a same class should have the same unique_id; different classes should have different unique_id's.
The simplest way to do this appears to be threading a singleton through the classes.
However, I don't know what's called when for static class members / things that happen before main.
(1) if you have a solution that does not involve using singleton, that's fine too
(2) if you have a solution that gives me a :
int unique_id();
that is fine too.
Thanks!
Have a class that increments it's ID on each creation. Then use that class as a static field in each object that is supposed to have an ID.
class ID
{
int id;
public:
ID() {
static int counter = 0;
id = counter++;
}
int get_id() { return id; }
};
class MyClass
{
static ID id;
public:
static int get_id()
{
return id.get_id();
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With