Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!ocsmd!jim From: jim@ocsmd.ocs.com (Jim Muth) Newsgroups: comp.lang.c++ Subject: Re: pointer to function Keywords: pointer function arguments Message-ID: <861@ocsmd.ocs.com> Date: 21 Mar 90 04:35:38 GMT References: <1990Mar19.233906.23149@elroy.jpl.nasa.gov> Reply-To: jim@ocsmd.ocs.com (Jim Muth) Distribution: usa Organization: Online Computer Systems, Inc. Lines: 50 In article <1990Mar19.233906.23149@elroy.jpl.nasa.gov> richard@ben.Jpl.Nasa.Gov (Conan the Barbarian) writes: >Yuck. That means the handler for a asynchronous server can >not use C++ strategies unless you have all of the server >source also. Double yuck. Who wants to re-write X, Motif, >Xview, Sunview, etc? Triple yuck Perhaps only a single yuck. A yuck and a half? There is a hack to get C++ member functions called from a system that uses C callbacks, as long as there is a way to hang a pointer off of the "server" object. For example, in say you wanted to define a SunView class, TkFrame, that could handle events: class TkFrame { public: TkFrame() { handle = 0; } Frame Create(); void EventHandler(Event*, void*); private: Frame handle; }; The trick is to associate a pointer to an instance of TkFrame with the SunView Frame, and to define an ordinary, non-member function as the callback: Frame TkFrame::Create() { handle = window_create(NULL, FRAME, WIN_CLIENT_DATA, this, WIN_EVENT_PROC, ::EventHandler, 0); return handle; } The ordinary function just yanks the pointer to a TkFrame out of the Frame and calls the member function: static void EventHandler(Frame handle, Event* event, void* arg) { TkFrame* frame = (TkFrame*) window_get(handle, WIN_CLIENT_DATA); frame->EventHandler(event, arg); } Not pretty, but it gets the job done. Jim Muth Online Computer Systems, Inc jim@ocsmd.ocs.com