On Mon, May 14, 2012 at 12:05:53PM -0700, H. S. Teoh wrote: > On Mon, May 14, 2012 at 08:40:30PM +0200, Stephen Jones wrote: > > I want an array of different classes of objects. I tried to > > subsume the differences by extending the classes under a single > > interface/abstract class/super class (3 different approaches) all > > to no avail as I could not access the public variables of the > > instantiated classes while storing them in an array defined by > > the interface/super class. [...]
Note also that while it's possible to downcast, being forced to downcast is often a sign of bad OO design, cf. the Liskov substitution principle: http://en.wikipedia.org/wiki/Liskov_substitution_principle If possible, one should always try to design the program in such a way that downcasting is not necessary. But, this being not an ideal world, downcasting is there for you to use when you have to: cast(Derived) will return a reference to the derived type if that particular object is actually an instance of Derived, otherwise it returns null (so you're safe from accessing non-existent members in an object that isn't actually an instance of Derived, which may happen when you're using a base class reference to access the object). T -- "Maybe" is a strange word. When mom or dad says it it means "yes", but when my big brothers say it it means "no"! -- PJ jr.