home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / gnu / g / help / 1224 < prev    next >
Encoding:
Text File  |  1992-09-13  |  3.4 KB  |  120 lines

  1. Newsgroups: gnu.g++.help
  2. Path: sparky!uunet!stanford.edu!ames!haven.umd.edu!darwin.sura.net!spool.mu.edu!caen!destroyer!ubc-cs!unixg.ubc.ca!kakwa.ucs.ualberta.ca!acs.ucalgary.ca!edstrom
  3. From: edstrom@hsc.ucalgary.ca (John Edstrom)
  4. Subject: Re: how to do this?
  5. Message-ID: <92Sep13.170150.28707@acs.ucalgary.ca>
  6. Sender: news@acs.ucalgary.ca (USENET News System)
  7. Date: Sun, 13 Sep 92 17:01:50 GMT
  8. References: <92Sep10.203359.4881@acs.ucalgary.ca> <1992Sep12.160053.28778@isy.liu.se>
  9. Nntp-Posting-Host: elmer.hsc.ucalgary.ca
  10. Organization: Neuroscience Division, U of Calgary School of Medicine, Calgary, Alberta, Canada
  11. Keywords: pointer to member
  12. Lines: 106
  13.  
  14. In article <1992Sep12.160053.28778@isy.liu.se> svan@isy.liu.se writes:
  15. >> What is wrong with the
  16. >> following constructions and how can I do it in g++?
  17. >>
  18. >> ----------
  19. >>
  20. >> class a {
  21. >> public: 
  22. >>    int x();
  23. >> };
  24. >>
  25. >> int a::x() { return 1; }
  26. >>
  27. >> main() {
  28. >>    printf("%x\n", (int(*))a::x);
  29. >> }
  30. >
  31. >You're trying to cast a pointer to member to a pointer to object
  32. >type. That can't be done according to the standards
  33. >documentation I've read over C++ (Annotated C++ Ref. Man. by
  34. >Ellis & Stroustrup). 
  35.  
  36. To a member function, not just a member.  It can be done because I
  37. encountered it while trying to port some code from AT&T C++ to g++.
  38. It just doesn't seem to be possible in g++.
  39.  
  40. >Pointer to members may even not be explicitly converted to void*!
  41. >
  42. >The reason for this is that pointers to members may be
  43. >a structure containing both addresses and offsets. There is
  44. >no point in allowing a cast to any ordinary pointer.
  45. >
  46.  
  47. There is if you want to choose different functions at a certain point
  48. in a program depending on current circumstances.  Sorting elements by
  49. different criteria, for example.
  50.  
  51.  
  52. The workaround I've discovered is as follows :
  53.  
  54. typedef int (a::*afp)( void );   // can not do it without typedef and
  55.                  // using intermediate variables
  56. class a {
  57. public:
  58.   int a(void);
  59.   int b(void);
  60.   int c(void);
  61. };
  62.  
  63. int a::a(void) { return 1; }
  64. int a::b(void) { return 2; }
  65. int a::c(void) { return 3; }
  66.  
  67. main() {
  68.   afp y = a::x;
  69.  
  70.   printf("%x\n", y);  //  this gives me the address of the member
  71.               //  function 
  72. }
  73.  
  74.  
  75. But still I can't execute the function directly like its seems I
  76. should be able to:
  77.  
  78. inline int a::do_something( afp x ) { return x(); }   // no way
  79.  
  80.  
  81. The only way I can get it to go is to have another member function
  82. like: 
  83.  
  84. int a::executor(cfp x) {
  85.   if (x == a)
  86.     return a();
  87.   if (x == b)
  88.     return b();
  89.   if (x == c)
  90.     return c();
  91. }
  92.  
  93.  
  94. Where to get the value from a passed member function pointer the usage
  95. is: 
  96.  
  97. inline int a::do_something(afp x ) { return executor(x); }   
  98.  
  99.  
  100. I know this is ugly and stupid and I hope there is a better way.
  101.  
  102.  
  103. >O----------------------------O----------------------------------O
  104. >| Jonas Svanberg             | Email: svan@isy.liu.se           |
  105. >| Div. of Information Theory O----------------------------------O
  106. >| Department of E.E. (ISY)   | "And Noah said to the animals:   | 
  107. >| Linkoping University       |  - Go out and multiply!          |  
  108. >| S-581 83 Linkoping         |  The snake said:                 |
  109. >| SWEDEN                     |  - But how can I? I'm an adder!" |
  110. >O----------------------------O----------------------------------O
  111. >
  112.  
  113.  
  114. JE
  115. --
  116.  RM 2104, HSc Building,  Div. Neuroscience
  117.  U. Calgary School of Medicine,  3330 Hospital Drive NW
  118.  Calgary, Alberta       T2N 3Y4
  119.  (403) 220 4493  (wk)
  120.