PDA

View Full Version : Is there any way to loop through all instances of a class?



RubyTigerSG
2nd April 2011, 11:26
Hi Gurus,

if I instantiated a class in multiple locations , is there any generic way I can loop through all the instances of that class?

Thanks,

RT

wysota
2nd April 2011, 12:34
No, there is not.

JohannesMunk
2nd April 2011, 14:35
There is none prebuilt. But you could maintain a static list of instances in the class constructor.

But depending on what you are trying to achieve and in what context this might be very bad design.

It's usually much better to maintain lists of your instances on the scope the list is going to be used.

For example a QGraphicsScene maintains a list of QGraphicsItems. As there can be multiple scenes in one application this is much better design than maintaining a static thus global list.

HIH

Johannes