home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!rational.com!stripe!rmartin
- From: rmartin@stripe.Rational.COM (Bob Martin)
- Subject: Re: Address of Member Functions
- Message-ID: <rmartin.728089937@stripe>
- Sender: news@rational.com
- Organization: Rational
- References: <pbender-250193124301@pbender.qualcomm.com>
- Date: Tue, 26 Jan 1993 23:12:17 GMT
- Lines: 46
-
- pbender@qualcomm.com (Paul Bender) writes:
-
- |Is there a reason why this will not valid?
-
- |class X {
- |private:
- | void g1(void);
- | void (*g)(void);
- |public:
- | X(void);
- | void f(void);
- |};
-
- |void X::f(void)
- |{
- | g = &g1; // line 12
- |}
-
- |main(void)
- |{
- | X x;
- |}
-
- |When I compile it using g++, I get the following error message:
- |test.C:14: assignment between incompatible pointer types
-
- This question, and questions like it, have got to be the most
- frequently asked questions in this group.
-
- g1 is not a function. It is a member function. void (*g)(void)
- declares g to be a pointer to a function, not a pointer to a member
- function. The two pointer types are incompatible.
-
- If you want 'g' to point to member functions of X then you must declare it
- as follows:
-
- void (X::*g)(void);
-
- Pointers to members and member functions are complicated beasties. I
- suggest you pore over a *good* C++ book to learn how they are used.
-
- --
- Robert Martin | Design Consulting | Training courses offered:
- R. C. M. Consulting | rmartin@rational.com | Object Oriented Analysis
- 2080 Cranbrook Rd. | Tel: (708) 918-1004 | Object Oriented Design
- Green Oaks, Il 60048| Fax: (708) 918-1023 | C++
-