home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18971 < prev    next >
Encoding:
Text File  |  1993-01-11  |  2.5 KB  |  72 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!rational.com!thor!rmartin
  3. From: rmartin@thor.Rational.COM (Bob Martin)
  4. Subject: Re: Callbacks in C++
  5. Message-ID: <rmartin.726794232@thor>
  6. Sender: news@rational.com
  7. Organization: Rational
  8. References: <1iikcuINN6fr@news.cerf.net> <rmartin.726677907@thor> <1iqqipINN11b@news.cerf.net>
  9. Date: Mon, 11 Jan 1993 23:17:12 GMT
  10. Lines: 60
  11.  
  12. hlf@nic.cerf.net (Howard Ferguson) writes:
  13.  
  14.  
  15. |If I derive from the ButtonCallback (supplied by the class library)
  16. |then I presume I will have to overwrite the ButtonHasBeenClicked() 
  17. |function which is a member of the ButtonCallback to do something like 
  18. |this 
  19.  
  20. |UserButtonCallback::ButtonHasBeenClicked(Buttonvoid *userInfo)
  21. |{
  22. |    switch (userInfo) {
  23. |    case SAVE : display->save_screen();
  24. |            break;
  25.  
  26. |    case ZOOM : display->zoom_screen();
  27. |            break;
  28. |}
  29.  
  30. |I can see where your suggestion of deriving from a pure virtual would
  31. |be very useful in the situation where the number and types of 
  32. |callbacks are restricted -- but does it provide any easy way of getting
  33. |the functionality required for the example just given ???????
  34.  
  35. I am not quite clear on what you mean here.  Let me reinterpret your
  36. statement and then give you an answer to what I think your question
  37. is.
  38.  
  39. You have two buttons in a user window.  Clicking on one saves the
  40. screen.  Clicking on the other zooms the screen.  Both of them have
  41. been created with "Callback" objects which are both instances of the
  42. class UserButtonCallback.  You have derived this calss from (say) the
  43. base class "ButtonCallback" which implements the pure virtual
  44. "ButtonHasBeenClicked" Method.
  45.  
  46. In the code above, you appear to be passing in "userInfo" which seems
  47. to be the identity of the button that was clicked.  Then you switch on
  48. this ID to decide what to do.
  49.  
  50. =-=-=-=-=-=-=-=-=-=-=-=-
  51.  
  52. A better solution is to derive two different classes from
  53. "ButtonCallback"  SaveButtonCallback and ZoomButtonCallback.  The Save
  54. button is created with the SaveButtonCallback instance.  The Zoom
  55. button is created with the ZoomButtonCallback instance.  In each, the
  56. "ButtonHasBeenClicked" met;hod is overriden to do the appropriate
  57. thing.
  58.  
  59.  
  60. class ZoomButtonCallback: public ButtonCallback
  61. {
  62.   public:
  63.     void ButtonHasBeenClicked() {display->ZoomScreen();}
  64. };
  65.  
  66.  
  67. --
  68. Robert Martin                        Training courses offered in:
  69. R. C. M. Consulting                       Object Oriented Analysis
  70. 2080 Cranbrook Rd.                        Object Oriented Design
  71. Green Oaks, Il 60048 (708) 918-1004       C++
  72.