home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19924 < prev    next >
Encoding:
Text File  |  1993-01-27  |  1.5 KB  |  58 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!rational.com!stripe!rmartin
  3. From: rmartin@stripe.Rational.COM (Bob Martin)
  4. Subject: Re: Address of Member Functions
  5. Message-ID: <rmartin.728089937@stripe>
  6. Sender: news@rational.com
  7. Organization: Rational
  8. References: <pbender-250193124301@pbender.qualcomm.com>
  9. Date: Tue, 26 Jan 1993 23:12:17 GMT
  10. Lines: 46
  11.  
  12. pbender@qualcomm.com (Paul Bender) writes:
  13.  
  14. |Is there a reason why this will not valid?
  15.  
  16. |class X {
  17. |private:
  18. |  void g1(void);
  19. |  void (*g)(void);
  20. |public:
  21. |  X(void);
  22. |  void f(void);
  23. |};
  24.  
  25. |void X::f(void)
  26. |{
  27. |  g = &g1;                // line 12
  28. |}
  29.  
  30. |main(void)
  31. |{
  32. |  X x;
  33. |}
  34.  
  35. |When I compile it using g++, I get the following error message:
  36. |test.C:14: assignment between incompatible pointer types
  37.  
  38. This question, and questions like it, have got to be the most
  39. frequently asked questions in this group.   
  40.  
  41. g1 is not a function.  It is a member function.  void (*g)(void)
  42. declares g to be a pointer to a function, not a pointer to a member
  43. function.  The two pointer types are incompatible. 
  44.  
  45. If you want 'g' to point to member functions of X then you must declare it
  46. as follows:
  47.  
  48.     void (X::*g)(void);
  49.  
  50. Pointers to members and member functions are complicated beasties.  I
  51. suggest you pore over a *good* C++ book to learn how they are used.
  52.  
  53. --
  54. Robert Martin       | Design Consulting    | Training courses offered:
  55. R. C. M. Consulting | rmartin@rational.com |  Object Oriented Analysis
  56. 2080 Cranbrook Rd.  | Tel: (708) 918-1004  |  Object Oriented Design
  57. Green Oaks, Il 60048| Fax: (708) 918-1023  |  C++
  58.