home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!news.cerf.net!nic.cerf.net!hlf
- From: hlf@nic.cerf.net (Howard Ferguson)
- Newsgroups: comp.lang.c++
- Subject: Re: Callbacks in C++
- Date: 11 Jan 1993 03:46:33 GMT
- Organization: CERFnet Dial n' CERF Customer Group
- Lines: 45
- Message-ID: <1iqqipINN11b@news.cerf.net>
- References: <1iikcuINN6fr@news.cerf.net> <rmartin.726677907@thor>
- NNTP-Posting-Host: nic.cerf.net
-
- In article <rmartin.726677907@thor> rmartin@thor.Rational.COM (Bob Martin) writes:
- >hlf@nic.cerf.net (Howard Ferguson) writes:
- >
- >>The FAQ for this group describes how to put wrapper functions around
- >>member functions so they can be called as callbacks from X libraries
- >>and the like. But if I am writting a c++ class library, surley I should
- >>be able to provide a cleaner mechanism to my customers. Is there
- >>a standard way of doing this???
- >
- >If you are designing a class library, you don't generally want to
- >provide callbacks to your users. Instead, you want to provide them
- >with interfaces that they can use to receive information from you.
- >These interfaces are abstract classes. Pure virtual functions are
- >declared which allow information to be passed from your class library
- >to user objects derived from the abstract classes. The users can
- >implement the derivatives of the pure virtuals as they see fit.
- >
- Does this derived function not just end up being a large
- switch statement -- which is exactly what you are trying to avoid.
- For example if the user has a display object with 2 members save_screen()
- and zoom_screen(). When the buttons for these pieces of functionality
- are created the application wants to tell the button that when it is
- selected then the save_screen() function should be called.
-
- If I derive from the ButtonCallback (supplied by the class library)
- then I presume I will have to overwrite the ButtonHasBeenClicked()
- function which is a member of the ButtonCallback to do something like
- this
-
- UserButtonCallback::ButtonHasBeenClicked(Buttonvoid *userInfo)
- {
- switch (userInfo) {
- case SAVE : display->save_screen();
- break;
-
- case ZOOM : display->zoom_screen();
- break;
- }
-
- I can see where your suggestion of deriving from a pure virtual would
- be very useful in the situation where the number and types of
- callbacks are restricted -- but does it provide any easy way of getting
- the functionality required for the example just given ???????
-
- hlf
-