Jump to content
  • Advertisement
Sign in to follow this  
ScottH87

Function Pointers

This topic is 3394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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 funcitons
void 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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!