Thread: Member Function Pointer...

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up Member Function Pointer...

    Well, this is starting to annoy me and now it is preventing my project from continuing on. I want to have member function pointers that can still access the members of the class. For example:

    Code:
    class FOO
    {
    private:
    	int a;
    
    public:
    	void TEST() { return a; }; // how do I get a pointer to this?
    };
    I know this isn't possible (without the function being static), but what other alternatives are there (not including global variables, if possible)?

    Thanks.

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Code:
    class FOO
    {
    private:
    	int a;
    
    public:
    	int TEST() { return a; }; // how do I get a pointer to this?
    };
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      FOO afoo;
      int (FOO::*fp)()= &FOO::TEST;
    
      (afoo.*fp)();
    
      return 0;
    }
    Last edited by m37h0d; 11-06-2009 at 07:40 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Unlike a normal pointer, which points at an absolute address, a "pointer to member" is relative to some object. A "pointer to member" carries no information about the object it will act on.

    On its own, therefore, a pointer to member is unusable. The second bit of information needed is the object it is relative to. Hence the need for "(anObject.*a_member_pointer)()" method of use.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem defining structure
    By MTK in forum C Programming
    Replies: 12
    Last Post: 09-08-2009, 03:26 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM