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

  1. Newsgroups: comp.lang.c++
  2. From: nikki@trmphrst.demon.co.uk (Nikki Locke)
  3. Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
  4. Subject: Re: Clarification of Callbacks in C++
  5. Reply-To: nikki@trmphrst.demon.co.uk
  6. References: <1il5pcINNblt@news.cerf.net> <1iikcuINN6fr@news.cerf.net>
  7. Distribution: world
  8. X-Mailer: cppnews $Revision: 1.30 $
  9. Organization: Trumphurst Ltd.
  10. Lines: 90
  11. Date: Mon, 11 Jan 1993 13:47:51 +0000
  12. Message-ID: <726785271snx@trmphrst.demon.co.uk>
  13. Sender: usenet@demon.co.uk
  14.  
  15. In article <1il5pcINNblt@news.cerf.net> hlf@nic.cerf.net (Howard Ferguson) writes:
  16. > I was to 
  17. > supply my colleges who are coding in C++ with some classes written in
  18. > C++ which require calbacks. My point is that I should be able to
  19. > provide something cleaner than what I must use if I am writting a
  20. > C library which may be used by C++ coders ( e.g. Xview).
  21.  
  22. It is pretty unusual to need callbacks as such in C++. Normally speaking, 
  23. you would provide an abstract base class containing a virtual function. 
  24. Users then derive their class from that abstract base, overriding the 
  25. virtual function to do whatever their callback function would do.
  26.  
  27. A typical example might be ...
  28.  
  29. class Shape {
  30. public:
  31.     virtual void draw() = 0;
  32.     };
  33.  
  34. A user creating a Rectangle (say) would derive from Shape. They would pass
  35. a Shape pointer to to your library functions, which would call draw for 
  36. that Shape whenever they want to draw it.
  37.  
  38. Of course, the situation can get more complex - e.g. when you want to 
  39. perform some arbitrary (unknown at compile time) action on some arbitrary 
  40. object in your library function.
  41.  
  42. I have found it convenient in these cases to provide a template class
  43. (which I call a Command) to encapsulate such a callback ...
  44.  
  45. #include <iostream.h>
  46.  
  47. // You write this bit
  48. class Comd {
  49. public:
  50.     virtual int doit() const = 0;
  51.     };
  52.  
  53. template <class X> class Command : public Comd {
  54.     X &to;
  55.     int (X::*f)(void);
  56. public:
  57.     Command(X &destination, int (X::*function)(void))
  58.     : f(function), to(destination) {}
  59.     int doit() const { return (to.*f)(); }
  60.     };
  61.  
  62. int libraryFunction(const Comd &command)
  63. {
  64.     // Do some internal library stuff
  65.     if(command.doit())
  66.         {
  67.         return 1;    // Do some stuff which depends on success
  68.         }
  69.     else
  70.         {
  71.         return 0;    // Do some stuff which depends on failure
  72.         }
  73. }
  74.  
  75. // The user writes this bit
  76. class Test {
  77.     int i;
  78. public:
  79.     Test(int x) : i(x) {}
  80.     int test1() { cout << i << " test1\n"; return 1; }
  81.     int test2() { cout << i << " test2\n"; return 1; }
  82.     };
  83.  
  84. main()
  85. {
  86.     Test t1(1), t2(2);
  87.     Command<Test> c1(t1, &Test::test1), c2(t2, &Test::test2);
  88.  
  89.     libraryFunction(c1);
  90.     libraryFunction(c2);
  91.     return 0;
  92. }
  93.  
  94. Obviously, this can easily be extended to allow the "callback" to take 
  95. arguments, or have different return values. I use a similar Command class 
  96. in my DOS text mode user interface toolkit, for attaching to buttons and 
  97. menu options - when the button is pressed (or the option is selected), the
  98. Command is executed. In this context, an enhancement which springs to mind 
  99. is undo-able Commands, which have a virtual undo function, and which 
  100. register themselves on an undo stack (or similar).
  101.  
  102. -- 
  103. Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
  104. trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
  105.