home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!rational.com!thor!rmartin
- From: rmartin@thor.Rational.COM (Bob Martin)
- Subject: Re: Callbacks in C++
- Message-ID: <rmartin.726794232@thor>
- Sender: news@rational.com
- Organization: Rational
- References: <1iikcuINN6fr@news.cerf.net> <rmartin.726677907@thor> <1iqqipINN11b@news.cerf.net>
- Date: Mon, 11 Jan 1993 23:17:12 GMT
- Lines: 60
-
- hlf@nic.cerf.net (Howard Ferguson) writes:
-
-
- |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 ???????
-
- I am not quite clear on what you mean here. Let me reinterpret your
- statement and then give you an answer to what I think your question
- is.
-
- You have two buttons in a user window. Clicking on one saves the
- screen. Clicking on the other zooms the screen. Both of them have
- been created with "Callback" objects which are both instances of the
- class UserButtonCallback. You have derived this calss from (say) the
- base class "ButtonCallback" which implements the pure virtual
- "ButtonHasBeenClicked" Method.
-
- In the code above, you appear to be passing in "userInfo" which seems
- to be the identity of the button that was clicked. Then you switch on
- this ID to decide what to do.
-
- =-=-=-=-=-=-=-=-=-=-=-=-
-
- A better solution is to derive two different classes from
- "ButtonCallback" SaveButtonCallback and ZoomButtonCallback. The Save
- button is created with the SaveButtonCallback instance. The Zoom
- button is created with the ZoomButtonCallback instance. In each, the
- "ButtonHasBeenClicked" met;hod is overriden to do the appropriate
- thing.
-
-
- class ZoomButtonCallback: public ButtonCallback
- {
- public:
- void ButtonHasBeenClicked() {display->ZoomScreen();}
- };
-
-
- --
- Robert Martin Training courses offered in:
- R. C. M. Consulting Object Oriented Analysis
- 2080 Cranbrook Rd. Object Oriented Design
- Green Oaks, Il 60048 (708) 918-1004 C++
-