Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!crdgw1!uunet!infonode!b17a!greg!allgood From: allgood@b17a!greg!allgood.ingr.com (Greg Allgood) Newsgroups: comp.lang.c++ Subject: C++ Limitation Keywords: C++ Message-ID: <1991May14.155950.4336@infonode.ingr.com> Date: 14 May 91 15:59:50 GMT Sender: usenet@infonode.ingr.com (Usenet Administrator) Reply-To: allgood@greg.b17a.ingr.com Organization: Intergraph Mapping Data Management Lines: 119 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. I believe this is a necessary capability to capture the full power of re-use. As another example, consider the items found in a dialog box. In some toolkits whenever an item (such as a radio button) is activated, there would be some common routine called to handle the event. The id of the item would be passed to the routine and a switch statement would be inside to handle events for that specific item. I would like to get away from this approach because there is a loss of information taking place. When an item is activated in some way, it knows about it and should know what to do by itself. By calling some common routine with a switch statement, I have to figure out what I knew already. I would like to create the dialog box and tell all of the items what they should do. Then all I have to do is display the dialog box and everything just works. This gives me some "slick" re-use opportunities as long as the arguments match between the objects which I am hooking up. Now obviously I would like to modify C++ to allow me to do this, but I have not come up with the way yet. Note that this does not require dynamic type checking although that would be a way to do it. I think that all I really need is some generic types for Objects and Messages. I have found a rather un-ellegant way using pointer-to-member functions. However, I am not sure how portable this method is. Basically, all objects which need to be hooked to something would have to derive off of an empty base class. Unfortunately, I have to do a cast. Here is a simplistic example: -------------- #include #include class Recipient { virtual void VirtualTableKludge() {} }; class View : public Recipient { public: View() {} void moved(int dp) {cout << "View moved by " << dp << endl;} virtual void moved_to(int p) {cout << "View moved to " << p << endl;} }; class ScrollBar { protected: int t; Recipient *obj; void (Recipient::*moved_to)(int t); void (Recipient::*moved)(int dt); public: ScrollBar(int _t) { t = _t; } void when_moved(Recipient *_obj, void (Recipient::*_moved_to)(int t), void (Recipient::*_moved)(int dt)) { obj = _obj; moved_to = _moved_to; moved = _moved; } void move(int dt) { t += dt; if (obj != NULL) (obj->*moved)(dt); } void move_to(int _t) { t = _t; if (obj != NULL) (obj->*moved_to)(t); } }; main() { ScrollBar sb(5); View v; sb.when_moved(&v, (void (Recipient::*)(int)) &View::moved_to, (void (Recipient::*)(int)) &View::moved); sb.move(-2); sb.move_to(10); } -------------- With AT&T C++ release 2.1, this works and prints out: View moved by -2 View moved to 10 I would greatly appreciate any comments or reasons why the above method is non-portable. I know that I am depending on the way pointer-to-member functions are done with AT&T, and I wonder how safe that is. -- ______________________________________________________________________________ | Greg Allgood | John 14:6 Intergraph Corporation | Huntsville, Alabama | Jesus saith unto him, I am the way, Mail Stop IW17A2 | the truth, and the life: no man cometh | unto the Father, but by me. (205) 730-7052 | allgood@greg.b17a.ingr.com | | _______________________________/ \____________________________________________