TITLE: Avoiding some multiple inheritance PROBLEM: [ I thought this snippet might be interesting since we are now starting to see inheritance relationships like that below in our Guide client applications. The suggestion below makes a lot of sense, especially if you view inheritance as a special case of delegation. -adc ] RESPONSE: rcarr@isosa1.estec.esa.nl (Richard Carr) L / \ / \ L1 L2 | | | | | A | | / \ | |/ \| M1 M2 Now I store objects of type M1 and M2 in container classes which are part of the purchased library. Various library routines supply pointers of type L* Because the objects contained are exclusively of class M1 or M2 I cast this pointer to A*. Using this to reference the public integer declared in A messes up. RESPONSE: rmartin@rcmcon.com (Robert Martin), 21 Dec 93 This will indeed screw up. At least until you start using a compiler that has RTTI. They you will be able to use dynamic_cast as follows: A* ap = dynamic_cast(lp); The reason that this won't work without RTTI is that the relationship between A and L (i.e. their respective locations within the storage of the object) is a function of M1 and M2. Since without RTTI the compiler has no way of knowing that the object is an M1 or M2, it has no way of knowing how to convert the L pointer to an A pointer. So it trusts you. When you cast the L* to an A* the compiler throws up its hands and says: "Well, he must know what he is doing" and so it treats the L object as though its memory were laid out like an A object. Clearly this is wrong since the object is really an M1 or M2 and has a very different memory layout than either an L or A would have. If you don't have access to a compiler with RTTI, then you might want to consider an alternate solution. Derive A from L. A contains your integer. A also contains a pointer to another L object. All the methods of L in A are overridden to delegate to the contained L object. A1 is derived from A and the L pointer is initialized to point to an M1 object. A2 is derived from A and the L pointer is initialized to point to an M2 object. Now you can safely downcast from L to A since there is no MI. Then you can query the integer you need. And the A1 and A2 derivatives will still behave as M1 and M2 objects through delegation.