Counting/tracking instances of an Object problem
I have an object which is rather complicated so i'm just going to make a simplified example of how this object keeps count of its instances:
Code:
class myObj {
static int numOfObj;
int objId;
//constructor
myObj() {
objId = ++numOfObj;
}
}
The objId representing the ID should always increment, and this works fine until I (need to) delete some objects later on.
So this is the path through my program which creates the problem:
1. Open Program
2. Autoload > 0 files found (nothing loaded)
3. Create 5 instances of myObject (IDs are 1,2,3,4,5)
4. Serialize and save to output files
5. Finished Processing -> Delete all files to start again
6. Create a few more instances (IDs are 6,7) //this is desired
7. Serialize and save to output files
8. Close Program (but not finished, so files not processed)
9. Open Program
10. Autoload > 2 files found (IDs are 6,7) //also desired
11. Create 1 instance of myObject (ID is 3) //PROBLEM
the ID in the above example becomes 3 because the program recognizes that the number of Objects is now 2 + new one = 3. But what I really want is a number that always increments and is unique.
after creating more instances the original files which were loaded (with ID 6,7 in the above example) will eventually be overwritten.
any suggestions on how I can fix this? really i'm looking for something which will work without having to save the number of orders to a file so that my program relies on a text file to start up correctly.