best way to make an id to class instances.
in
Programming Questions
•
1 year ago
How to make an incrementing id to every class instance without a global variable nor passing it as a parameter?
I'd like to not depend on me to keep tracking of how many instance has been created or deleted. And I think "id" should be a class var to avoid changing it after creation (private?)
I was reading about C++, and it seams that the way to do it in C++ would be "static int id". But static gives an error:
"The field id cannot be declared static; static fields can only be declared in static or top levels types"
Well... This does not help me...
Below the two ways i figured and don't want to use...
- int _id = 100; // using a global var
- Foo[] test = new Foo[10];
- void setup()
- {
- for (int i = 0;i< test.length; i++ )
- {
- test[i]=new Foo(i); // passing it
- }
- }
- void draw() {
- }
- class Foo
- {
- int id;
- int name;
- // int innerId; // i'd like it here..
- Foo(int _name)
- {
- name = _name;
- id = ++_id;
- println("this instance name is "+name+" with the id "+id);
- }
- }
1