Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!uwm.edu!rpi!zaphod.mps.ohio-state.edu!usc!samsung!uunet!brunix!sdm From: sdm@cs.brown.edu (Scott Meyers) Newsgroups: comp.lang.c++ Subject: Re: indirect message passing Message-ID: <41462@brunix.UUCP> Date: 31 May 90 18:20:19 GMT References: <1990May31.131937.25923@aucs.uucp> Sender: news@brunix.UUCP Reply-To: sdm@cs.brown.edu (Scott Meyers) Organization: Brown University Department of Computer Science Lines: 44 In article <1990May31.131937.25923@aucs.uucp> 880716a@aucs.uucp (Dave Astels) writes: >I am wondering if it is possible to pass messages in C++ indirectly. >Let me explain: say I have a class X which has a member function foo(). >Also, I have a variable y of type X. Rather than invoking y.foo() directly >I want to store the information for later use. ie. I want to set up a >callback (using X11 terminology).. to store the information that X::foo() >is to be called for the object y, then do the call later (in response to >some event, for example). I have done this in a UI I developed in Turbo >Pascal 5.5. I have since bought Zortech C++, and wish to eventually >rewrite the UI in C++. The technique in TP was implemented using generic >pointers (to the object and the method entry point), and some assembly to >do the actual method invocation (supplying the Self/this parameter, etc). Try this -- it works fine under cfront 2.0 (no assembly required): #include class X { public: void foo() { cout << "Foo!\n"; } }; main() { cout << "Declaring an X called y...\n"; X y; cout << "Calling y.foo() directly..."; y.foo(); cout << "Declaring pointers...\n"; X *py; py = &y; void (X::*pxmf)(); pxmf = X::foo; cout << "Calling y.foo() indirectly..."; (py->*pxmf)(); } For details, see Lippman, p. 213ff. Scott sdm@cs.brown.edu