# Function Pointers

## Recommended Posts

ScottH87    112
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?

##### Share on other sites
ScottH87    112
Can you explain the differance then please? Or direct me to a website that can?

##### Share on other sites
K1nG Gr4H4m    100
If I remember correctly, you need to do something like: pDecisionFunciton = &Character::Sleep;

##### Share on other sites
Evil Steve    2017
Quote:
 Original post by ScottH87Can 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 Gr4H4mIf I remember correctly, you need to do something like: pDecisionFunciton = &Character::Sleep;
Only if it's a static member function in another class.

##### Share on other sites
Cornstalks    7030
Quote:
 Original post by ScottH87Can you explain the differance then please? Or direct me to a website that can?

##### Share on other sites
ScottH87    112
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?

##### Share on other sites
Evil Steve    2017
Quote:
 Original post by ScottH87Ok 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?

##### Share on other sites
ScottH87    112
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

##### Share on other sites
polypterus    100
Try:

pDecisionFunciton = &Character::Sleep;

This is what I typically use.

##### Share on other sites
vetroXL    295
have you looked at boost functions? why reinvent the wheel?

##### Share on other sites
Try this and see if it does you any good: :)

Character.h
#define CALL_MEMBER_FUNC(object,ptrToMember)  ((object).*(ptrToMember))()class Character{	public:	    Character(void);	    ~Character(void);			    void Sleep();	 // All the other funcitons	 private:			            void (Character::*pDecisionFunciton)();};

Character.cpp
pDecisionFunciton = &Character::Sleep;	CALL_MEMBER_FUNC(*this, pDecisionFunciton);

##### Share on other sites
jyk    2094
Quote:
 Original post by Shadow WolfTry this and see if it does you any good: :)Character.h*** Source Snippet Removed ***Character.cpp*** Source Snippet Removed ***
No need for macros:

template < typename T >void CallMemberFunction(T& object, void (T::*function)()){    ((object).*(function))();}// ...pDecisionFunction = &Character::Sleep;	CallMemberFunction(*this, pDecisionFunction);

##### Share on other sites
ScottH87    112
Quote:
 Original post by Shadow WolfTry this and see if it does you any good: :)Character.h*** Source Snippet Removed ***Character.cpp*** Source Snippet Removed ***

It compiles fine now, but It doesnt actually call the function its pointing to. So i guess this is a problem with the macro? Can anyone help here?

##### Share on other sites
jyk    2094
Quote:
 It compiles fine now, but It doesnt actually call the function its pointing to. So i guess this is a problem with the macro? Can anyone help here?
It should work (although, again, I wouldn't recommend using a macro).

How do you know the function isn't being called? Can you post your code?

##### Share on other sites
ScottH87    112
Sorry it was my mistake.

I'd missed the brackets at the end of the macro.

##### Share on other sites
visitor    643
#define CALL_MEMBER_FUNC(object,ptrToMember)  ((object).*(ptrToMember))

template < typename T >void CallMemberFunction(T& object, void (T::*function)()){    ((object).*(function))();}

I'm not entirely convinced either of those has any purpose, seeing that they both make you type more to achieve something relatively simple, and the syntax for calling member function through the pointer is unmistakeable for anything else because of the special .* operator.

If it is for inexperience programmers that may not be familiar with the syntax, one could show that to them, and if they don't get it they should be prohibited from using pointers to methods.

Besides, wouldn't you need a whole set of overloads in case the method takes any parameters?

---------

Or perhaps these are for providing a simple function-call like syntax (for who knows which templated use). But then shouldn't these be a struct with the () operator and doesn't std::mem_fun_ref already do this (and boost::mem_fn even better)?

##### Share on other sites
jyk    2094
Quote:
 Original post by visitor *** Source Snippet Removed ****** Source Snippet Removed ***I'm not entirely convinced either of those has any purpose, seeing that they both make you type more to achieve something relatively simple, and the syntax for calling member function through the pointer is unmistakeable for anything else because of the special .* operator.If it is for inexperience programmers that may not be familiar with the syntax, one could show that to them, and if they don't get it they should be prohibited from using pointers to methods.Besides, wouldn't you need a whole set of overloads in case the method takes any parameters?---------Or perhaps these are for providing a simple function-call like syntax (for who knows which templated use). But then shouldn't these be a struct with the () operator and doesn't std::mem_fun_ref already do this (and boost::mem_fn even better)?
My intent was mainly to demonstrate that there's no reason to use macros for this. To be sure, the shortcut is of limited usefulness, and is made largely irrelevant by the tools that you mentioned (boost::function, boost::bind, etc.).

I don't know that such a convenience function would be *completely* pointless in all situations, but honestly I don't feel strongly enough about it to try and make a case for it :) Really, the goal was just to demonstrate to the OP how to do it without using macros if, for whatever reason, the OP does want to use a shortcut of this sort.