My brother "wants to do a class with a sequential auto incrementing index" ..
IN SHORT ( because it's already well explained in there ) look a this post/answer :
https://stackoverflow.com/questions/741 ... 0_74181334
But just to re-explain let's say he as a class :
Code: Select all
class Id
{
public:
Id()
{
id = unique_id++;
}
int get_id()
{
return id;
}
private:
int id;
static int unique_id;
}
Initialize static member in the source file :
static int Id::unique_id = 0;
Create objects :
Id id1; // id1.get_id() will return 1
Id id2; // id2.get_id() will return 2
Id id3; // id3.get_id() will return 3
Now ESPECIALLY THIS PART :
Code: Select all
Create objects :
Id id1; // id1.get_id() will return 1
Id id2; // id2.get_id() will return 2
Id id3; // id3.get_id() will return 3
Code: Select all
Create objects :
Id id1,id2,id3;
I mean that thing "only guarantees that all indexes will be different" but you can never tell "which one is the first and which one is the last" as
the compiler could create id3,id2,id1 and/or any order that could be.
Or "have I been told wrong ?" ..
To me, as I know things, I *THINK* ( and even here I am not 100% sure ) that the ONLY way "to force an order" would be to do :
Code: Select all
Create objects :
Id *id1 = new Id;
Id *id2 = new Id;
Id *id3 = new Id;
I read somewhere about "Windows Smart Pointers" they seems someway "guaranteed" to be created in an order and destoryed in reverse order but I think that's an MS only thing ?
I don't know I am NOT so convinced of that StackOverflow reply.
Anyone knows better ?


