Function Pointers

Started by
16 comments, last by Zakwayda 14 years, 11 months ago
I'm trying to create a function pointer so that I can set it to whatever function I want during the code and then simply call the function pointer when i want to call the function. I'm having a little trouble in trying to set it up though. This is basically what I have so far:- Character.h

class Character
{
public:
Character(void);
~Character(void);
//All the other funcitons
void Sleep();

private:
void (*pDecisionFunciton)(void);
//All the other variables
};

Character.cpp

pDecisionFunciton = &Sleep;

This is giving me the compile error
: error C2276: '&' : illegal operation on bound member function expression
From the website I've been looking at this synatax is correct, but then again the website I've been looking at havent used them in classes...is this the correct way to be going about it?
Advertisement
A pointer to a member function is not a function pointer.

Stephen M. Webb
Professional Free Software Developer

Can you explain the differance then please? Or direct me to a website that can?
If I remember correctly, you need to do something like: pDecisionFunciton = &Character::Sleep;
Quote:Original post by ScottH87
Can you explain the differance then please? Or direct me to a website that can?
The page Bregma linked to will tell you. Basically a member function has a hidden "this" pointer.

Quote:Original post by K1nG Gr4H4m
If I remember correctly, you need to do something like: pDecisionFunciton = &Character::Sleep;
Only if it's a static member function in another class.
Quote:Original post by ScottH87
Can you explain the differance then please? Or direct me to a website that can?

Did you even read the link Bregma gave? Did you try google? This was the third result for this search. Bregma's link is a must read though.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Ok I read Bregmas link, but I'm still having some issues when it comes to setting the address of the pointer.
This is what I have now:-

Character.h
#define CALL_MEMBER_FUNC(object,ptrToMember)  ((object).*(ptrToMember)) class Character{public:Character(void);~Character(void);//All the other funcitonsvoid Sleep();private://All the other variables};typedef void (Character::*MemberFunctionPointer)(void)  const;


Character.cpp
MemberFunctionPointer pDecisionFunciton;pDecisionFunciton = &Sleep;


And still getting the error
: error C2276: '&' : illegal operation on bound member function expression


Is there something that I'm missing?
Quote:Original post by ScottH87
Ok I read Bregmas link, but I'm still having some issues when it comes to setting the address of the pointer.
This is what I have now:-

Character.h
*** Source Snippet Removed ***

Character.cpp
*** Source Snippet Removed ***

And still getting the error
*** Source Snippet Removed ***

Is there something that I'm missing?
What about doing it without the & ? (I forget the exact syntax for member function pointers offhand).
And is the code in "Character.cpp" in a member function of the class?
It still doesnt work without the address operator and yeah the code I gave in Character.cpp is in a member function of the Character class
Try:

pDecisionFunciton = &Character::Sleep;

This is what I typically use.

This topic is closed to new replies.

Advertisement