How many instance of the class is initiated?
Mostrar comentarios más antiguos
Hello
I am just writing an object oriented code in Matlab. I need every object of my classes to know how many instances of that particular class has been made. I have tried persistent data variables or static methods. But no use! I appreciate any help.
Thank You
Hadi
5 comentarios
Matt J
el 24 de Mayo de 2013
I have tried persistent data variables or static methods. But no use! I appreciate any help.
Help with what? Why aren't persistent variables a good solution?
per isakson
el 24 de Mayo de 2013
handle or value classes?
Sean de Wolski
el 30 de Mayo de 2013
Why do you need this?
Hadi Hajieghrary
el 30 de Mayo de 2013
Sean de Wolski
el 30 de Mayo de 2013
Then why not have these ovbjects be children of a parent object like in handle graphics?
Respuestas (2)
Sean de Wolski
el 30 de Mayo de 2013
2 votos
Here is how I would attack this, (and let me reiterate, I don't know why you need this, there is probably a better way).
I would have singleton class:
That stores members of the other class. Let's call the singleton class, S.
Now you create one of your objects that needs to know about others, let's call it K. K first finds if an object of class S exists, if it does not, then it creates one. If it does, it calls a static method of this S object that bumps up the count. It also, adds a listener to S listening for it's own being deleted event. Now if this object of class K is deleted, the S object can remain up to date.
Alternatively, you could use findobj as is described here to look for some dummy property that all K objects have:
This is probably the easier way to do this.
2 comentarios
Hadi Hajieghrary
el 30 de Mayo de 2013
Sean de Wolski
el 30 de Mayo de 2013
No, the static method of the singleton object.
Hadi Hajieghrary
el 30 de Mayo de 2013
0 votos
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!