is there any way to make all derived classes count their instances?How (write code in one of C++, C#, Java)?
Imagine I\'ve got access to the root class (e.g. object), an
It strikes me that if you if you actually want the injected/whatever class to actually do something useful then you must couple it in some way to the original classes, whether it's by inheritance or direct coupling with method calls. Otherwise you've just got two wheels spinning independently.
The only alternative I can think of is to use a factory pattern that can count creations for you -- but you'd have to hack something to count decrements, like handing the object back to the factory explicitly.