Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!fernwood!lia!jgro From: jgro@lia (Jeremy Grodberg) Newsgroups: comp.lang.c++ Subject: Re: C++ Limitation Message-ID: <1991May20.002653.6871@lia> Date: 20 May 91 00:26:53 GMT References: <1991May14.155950.4336@infonode.ingr.com> Reply-To: jgro@lia.com (Jeremy Grodberg) Lines: 38 In article <1991May14.155950.4336@infonode.ingr.com> allgood@greg.b17a.ingr.com writes: >Hello, > > I apologize if you have seen multiples of this article - you know how >things can happen. > > I would like to have the capability of telling some object A, to send >message M to an object B where M and B are not specifically known to A. >For example, I would like to create a ScrollBar and tell it "Hey, when >you get scrolled to a new location, send a 'move_to' message to this >instance of class View". I can do this in C++ if the ScrollBar class knows >about the View class, but I want to create a generic ScrollBar which >can send messages to classes which will be defined later. This is what multiple inheritance is for. After you write your ScrollBar class without any specific recipient class in mind, the class will end up with a bunch of messages it wants to send. In order to be sure that those messages are all understood by the recipient, you must ensure that the recipient has member functions for all of them. The way to do this in C++ is to create a class, say ScrollBarReceiver, which has virtual member functions for all the messages that ScrollBar sends. Then any class that wants to be controlled by a ScrollBar can inherit from that class, in addition to any other classes it wants, and override the functions for the messages it wants to handle specially. ScrollBar then operates on objects of class ScrollBarReceiver. This is the only way to ensure at compile time (without an extremely intelligent compiler) that all the messages ScrollBar sends will be understood by the recipient. Note that this is one of the most important areas of divergence between C++ and Objective C. In Objective C, you could send messages to the objects without a compiler-given guarantee that the object will know what to do with them. Obviously there are trade-offs between the two approaches, and some people prefer one over the other, but I am all for keeping strong saftey checking in C++. If you don't like it, use Objective C. -- Jeremy Grodberg "Show me a new widget that's bug-free, and I'll show jgro@lia.com you something that's been through several releases."