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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!enterpoop.mit.edu!senator-bedfellow.mit.edu!bloom-picayune.mit.edu!ceci.mit.EDU!jud
  3. From: jud@ceci.mit.EDU (Judson Harward)
  4. Subject: Pointer to members
  5. Message-ID: <1993Jan5.230617.23557@athena.mit.edu>
  6. Sender: news@athena.mit.edu (News system)
  7. Nntp-Posting-Host: ithake.mit.edu
  8. Organization: "Center for Educational Computer Inintiatives at MIT"
  9. Date: Tue, 5 Jan 1993 23:06:17 GMT
  10. Lines: 80
  11.  
  12.  
  13.     The Annotated C++ Reference Manual hedges on the issue of
  14. generic pointers to members on p. 70:
  15.  
  16.     "There is no equivalent to void * for pointers to members.
  17. This implies that casting of pointers to pointers to members must
  18. be used if a generic 'pointer to any member of any class' is needed."
  19.  
  20.     That sounds fairly definite.  But then their proposed work around uses
  21. the following typedef:
  22.  
  23.     typedef void Z::* any_ptom;
  24.  
  25. any_ptoms are not used in the ARM example, but *any_ptoms are.
  26. Is there any reason not to support a generic "pointer to any member of any
  27. class" such as an any_ptom?
  28.  
  29.     The following code works on Sun's version of AT&T 2.1:
  30.  
  31. #include <stdlib.h>
  32. #include <iostream.h>
  33.  
  34. class Dummy;
  35. typedef void Dummy::*any_ptom;
  36.  
  37. class X
  38. {
  39. public:
  40.   int i;
  41.   X(int j) : i( j ) {};
  42. };
  43.  
  44. class Y
  45. {
  46. public:
  47.   double d;
  48.   Y(double e) : d( e ) {};
  49. };
  50.  
  51. main()
  52. {
  53.   X x(1);
  54.   Y y(2.0);
  55.   int mI;
  56.   double mD;
  57.   int X::* ptmX;
  58.   double Y::* ptmY;
  59.   any_ptom *paptm;
  60.   any_ptom aptm;
  61.  
  62.   ptmX = &X::i;
  63.   paptm = (any_ptom *) &ptmX;
  64.   mI = x.*(*(int X::**) paptm);
  65.   cout << mI << '\n';
  66.   ptmY = &Y::d;
  67.   paptm = (any_ptom *) &ptmY;
  68.   mD = y.*(*(double Y::**) paptm); 
  69.   cout << mD << '\n';
  70.  
  71. // I don't think the following is supposed to work.
  72. // Probably shouldn't use it.
  73.   aptm = (any_ptom) &X::i;
  74.   paptm = &aptm;
  75.   mI = x.*(*(int X::**) paptm);
  76.   cout << mI << '\n';
  77. }
  78.  
  79.     Is the success of the last portion using aptm = (any_ptom) just
  80. due to a lax compiler?
  81.  
  82.     Any comments would be appreciated.  Thanks in advance.
  83.  
  84. -- 
  85. Jud Harward
  86. AthenaMuse Software Consortium
  87. Center for Educational Computing Initiatives
  88. M. I. T.
  89.  
  90. jud@ceci.mit.edu
  91. (617-646-8045)
  92.