Path: utzoo!attcan!uunet!cs.utexas.edu!usc!apple!lsr From: lsr@Apple.COM (Larry Rosenstein) Newsgroups: comp.sys.mac.programmer Subject: Re: Multiple Inheritance for HandleObjects in C++ Message-ID: <8925@goofy.Apple.COM> Date: 29 Jun 90 16:15:47 GMT References: <15132@reed.UUCP> Organization: Apple Computer, Inc. Lines: 28 In article <15132@reed.UUCP> bowman@reed.UUCP (Eric Bowman) writes: >I've just discovered, to my horror, that you can't create HandleObjects >with multiple base classes in MPW C++. Are there any work arounds for this? >Anyone know why this is the case? The reason is that the standard implementation of C++ multiple inheritance requires that you pass a pointer to the middle of an object's storage. That's because each class' method is compiled to access fields using fixed offsets. (If you look at the generated C code for a native C++ class, you will see that it adds an offset to the object pointer before calling a method.) Methods of HandleObject subclasses expect to be passed a handle, and it's not possible to pass a handle that refers to the middle of an object. Plus you have to worry about the handle moving if the heap compacts. One workaround is to inherit from a primary class, and delegate some functions to to instances of the secondary classes. In other words, instead of inheriting from A & B, you inherit only from A, and keep an instance of B around. Methods that need to access B's functionality do so by sending a message to the instance of B. -- Larry Rosenstein, Object Specialist Apple Computer, Inc. 20525 Mariani Ave, MS 46-B Cupertino, CA 95014 AppleLink:Rosenstein1 domain:lsr@Apple.COM UUCP:{sun,voder,nsc,decwrl}!apple!lsr