home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / windows / openloo / 3624 < prev    next >
Encoding:
Internet Message Format  |  1992-08-27  |  5.4 KB

  1. Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uwm.edu!ogicse!qiclab!nosun!wnielsen
  2. From: wnielsen@nosun.West.Sun.COM (Walter Nielsen - SUN Portland)
  3. Newsgroups: comp.windows.open-look
  4. Subject: Re: Q: C++ and callback problems.
  5. Message-ID: <1992Aug27.172544.12548@nosun.West.Sun.COM>
  6. Date: 27 Aug 92 17:25:44 GMT
  7. Article-I.D.: nosun.1992Aug27.172544.12548
  8. References: <1737@ernohb.UUCP> <1992Aug24.171121.27530@isy.liu.se>
  9. Organization: Sun Microsystems, Beaverton Oregon
  10. Lines: 101
  11.  
  12. Its not all that complicated, cfront adds an hidden parameter to all member
  13. function calls.  Namely the 'this' pointer.  This doesn't happen in static
  14. functions but of course static member functions have their limitations.
  15. The easiest way is to define callbacks as regular functions and make
  16. them friends of your class.   Or alternatively have the callback be
  17. regular functions which call the object's member function which does
  18. the work.   Typically one would use the client_data field to get
  19. the object's 'this' pointer in the callback.
  20.  
  21. Object-oriented purists may object but this approach works.
  22.  
  23. -Walter Nielsen
  24. Sun Microsystems
  25.  
  26. > > I have problems with c++ and callbacks. My intention was to create a window
  27. > > class which contains one or more callback functions, implemented as class
  28. > > methods. This doesn't work properly. Somewere along the way ( probably down in
  29. > > Xlib ) the callback parameters are LOST i.e. if the callback funktion is NOT
  30. > > declared as a static. This is not what i want but it seems to be the only way 
  31. > > to overcome this strange behaviour. The static function will be common to all
  32. > > instances of this object. I will not be able to identify from wich instance the
  33. > > callback originated. I have tried this in OLIT and XVIEW as well and found the 
  34. > > same problems. I have tried it using X11R4 along with SUN's c++ compiler on a 
  35. > > SUN (IPX).  Here comes a simple sample program that works, but not the way i 
  36. > > want (without static).
  37. > I have had the same problem in a couple of projects now...
  38. > I have some comments:
  39. > 1. In your example program you choose to send the pointer-to-memberfunction
  40. >    as a callback. These pointers are *not* ordinary c-pointers in that they
  41. >    may not even occupy the same space as an ordinary pointer. Usually (I
  42. >    think) it is some sort of structure. There are no casts to ordinary 
  43. >    pointers to functions which are garanteed to work. So xview/olit gets
  44. >    a structure but thinks it's an ordinary functionpointer. The pointer
  45. >    it gets is more likely to point to a jumptable or something... It isn't
  46. >    a problem with XLib or something. The problem is a difference of types.
  47. >  
  48. > 2. Also these pointers do not contain any instance-information, so even if the
  49. >    package knew how to handle the pointer-to-memberfunction, (It had to be a
  50. >    C++-package for that) it doesn't know how to deliver the hidden <this>-
  51. >    parameter which every nonstatic memberfunction need. So when registering 
  52. >    a callback, the object too must be sent somehow. Strong typing doesn't 
  53. >    make this an easy task. The class of the object and the type of the
  54. >    pointer-to-member must agree in a certain way.
  55. >    
  56. > 3. So, what one need is to be able to extract a pointer-to-member-of-specific-
  57. >    instance. The designers of C++ have deliberately chosen not to have such
  58. >    "first class objects" in their language. For now (ever?) it isn't possible
  59. >    to implement callbacks in C++ the way you (and me) would like. Some
  60. >    have tackled this problem. I e Borlands Turbo C++ for windows have some
  61. >    weird extension to the language that enables direct calls to specific
  62. >    objects. I don't like that ;-(
  63. > 4. With pure C++-toolkits one often overcome this problem by making one's classes
  64. >    inherit from the toolkitclasses. Since some of the memberfunctions in
  65. >    these classes are virtual you could shadow those and receive calls in
  66. >    that way. I e you make your own button inherit from the toolkit Button and
  67. >    defines your own buttonPressed()-memberfunction. See the FAQ for such
  68. >    toolkits.
  69. > CONCLUSION:
  70. > In C++, as it is defined, it's impossible to have callbacks the way which
  71. > seems natural. You (and I) will have to do with these static functions,
  72. > which of course could extract the object from some sort of userassignable
  73. > data in the toolkit-object, and call the appropriate method in the object.
  74. > Such static functions would act as some sort of stubs.
  75. > Maybe one should let the static function itself be the real handler
  76. > in the object, but then you have to write xxxx. in front of every
  77. > membercall...
  78. >      
  79. > References:
  80. >     
  81. >    "The annotated C++ reference manual" (AT&T 1990)
  82. >     Margaret A. Ellis & Bjarne Stroustrup
  83. >    5.4 page 70-71 for a discussion on pointer-to-memberfunctions
  84. >    4.8 page 38-39 casting pointers to members.
  85. >    There is a 2nd ed. out now, I guess these side# are obsolete...
  86. > ---
  87. > I'm using xview myself and have given up the idea of having a nice
  88. > and pure objectoriented system without old-c-style functions :-)
  89. > --------------------------------------------------------------
  90. >  Jonas Svanberg
  91. >  Department of Electrical Engineering
  92. >  Linkvping University
  93. >  S-581 83 Linkvping, Sweden             Email: svan@isy.liu.se
  94. > --------------------------------------------------------------
  95. -- 
  96.  
  97.               /\        
  98.              \\ \       Walter Nielsen
  99.             \ \\ /      Sun Portland - "Where the trees grow bigger"
  100.