Since the identifier must be unique, you need to make sure that two instances never get the same identifier. In addition, none of the outer classes should interfere with the creation of the UID.
First you define a static field in your class:
class Data { private: static int newUID; (...) };
Then, after creating each instance, it should get a new ID value and increment the newUID counter:
class Data { (...) const int uid; public: Data() : uid(newUID++) { } int GetUid() { return uid; } };
Someone does not have access to the internal newUID, except for the class, an identifier is created automatically for each instance, and you (almost 1 ) are sure that neither of the two instances will have the same identification number.
1 If you do not create a little more than 4 billion copies
Spook source share