Thread: Pointer to Function

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    48

    Pointer to Function

    I cannot get pointers to functions to work. I'm still using the teach yourself book, and I have copied various samples letter for letter but I am still getting the same error message...

    I just have one class for this example, 1 object and one pointer to access one of the member access methods...

    In the class declaration I have...

    Code:
    int GetAge() {return itsAge;}
    Then in main I have...

    Code:
    int (Cat::*pFunc) ();
        int(*pFunc)();
        Cat Bob(5);
        pFunc = Cat::GetAge;
        Bob.*pFunc;
    I get this error...

    Code:
    line 26 error: invalid use of non_static member function 'int Cat::GetAge()'
    I have tried every which way I can figure using the examples in the book but I always get this error, what am I doing wrong?

    EDIT - Sorry missed some lines in my first post..
    Last edited by MattJ812; 12-31-2010 at 04:18 PM. Reason: Missed out some lines of code

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    GetAge is a member function -- that means it's a member of every instance. Bob has a getAge member function, and if you declared another cat called Felix, Felix would have his own getAge member function, etc. There's not one getAge function for the whole class (which is what Cat::GetAge would be, something that belongs to the class-as-a-whole). So you need to declare pFunc as a member function pointer, which is what that first line does. I don't know exactly how you got away with re-declaring pFunc, but don't.

    (EDIT: I think my sentence ran away with me in the middle there. My point, perhaps better worded: you can't assign getAge to a "normal" function pointer, since it's a member function. You have to assign it to a member function pointer, and specifically a member-of-Cat function pointer.)

    More edit: After testing, it looks like you definitely need & to form a pointer-to-member function. I can make the following work:
    Code:
        int (Cat::*pFunc)();
        Cat Bob(5);
        pFunc = &Cat::getAge;
        std::cout << "Bob is " << (Bob.*pFunc)() << std::endl;
    There may be extraneous parentheses, but safety first.
    Last edited by tabstop; 12-31-2010 at 05:17 PM.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    48
    What is the & doing exactly? Using the memory address of Bob?

    The chapter I am on was talking a lot about static member data so I understand what your saying about individual get age functions, I guess it didn't sink in.

    If I had 5 cats and wanted to assign the pointer to the getage function how would I go about differentiating which Cat I am reffering to when I assign the pointer?

    That seems a bit redundant, I may be barking up the wrong tree on the implementation of the pointer, I'm just trying to get a fix on how it works.
    Like if 5 cats had 5 member functions and I used the pointer for the 5 functions, how would the compliler know which cat I'm on about?

    I guess Ill go back and reread the chapter again.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No no, you do have to use Cat::getAge when you assign the pointer. You just have to have a Cat object when you call the thing. I think I lead you astray there.

    And & theoretically means "address of" (that's how you point to things, after all, is show where they are located), but what "address" really means in this context is up to the implementation.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    48
    I am a little confused on the context here, I understand the address of operator, and I think I understand needing an existing object to call the function.

    Does this simply mean, pFunc will call getage aslong as I have atleast one cat object and I declare which cat by whichevercat.*pFunc?

    I still don't understand how the & works in this case, does it just make sure theres an existing cat?

    I understood everything in the book until I got to pointers and nodes, they really mess with my head for some reason.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Most likely, here, "address of" is not an actual address, but an offset of. As a completely made up example, it might be +8, meaning that when you call whichevercat.*pFunc, you start at where whichevercat is, go forward 8 bytes, and there you find the information for the particular member function. But the offset is the same for all cats.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    48
    I think I get it now, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM