Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BradDaBug

Pointer to a function in a class

This topic is 6239 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

How do I create a pointer to a function in a class? when i try to assign the pointer function to the real function i get errors: error C2440: ''='' : cannot convert from ''int (__thiscall clsButton::*)(void)'' to ''int (__cdecl *)(void)'' heres the code for the class: class clsButton { public: RECT SrcRect; RECT DestRect; int Type; int (*clsButton::ButtFunc)(void); int NewGame(void); int SaveGame(void); int ExitGame(void); }; Heres the code i''m using to create the pointer: Button.ButtFunc = Button.NewGame; Button is an instance of clsButton, btw. any help?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I think that you need to assign the pointer to function with
Button.ButtFunc = Button::NewGame(). Also should
int (*clsButton::ButtFunc)(void); perhaps
be int (clsButton::*ButtFunc)(void); I''m a little unfamiliar with pointers to member functions - there are usually nicer ways to do the same thing. (Pointers to functions is very C, where as virtual, overloaded & inherited functions are very C++)

However, this approach doesn''t seem vary clean to me. Perhaps you should have an int variable to store which function your button performs and use a switch statement to invoke the correct behaviour (ie a member function called pressButton that switches on the int variable and calls NewGame etc). If you''re not worried about the overhead of a virtual function, then have an abstract base class button & subclass it with 3 child buttons that perform NewGame etc tasks.

Any comments to brad_beveridge(NO SPAM)@hotmail.com

Cheers
Brad

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hey - you have the same name as me!!

Cheers
Brad

Share this post


Link to post
Share on other sites
Member function pointers are declared:

returntype (Class::*MemberFunc)(params)

So, as Anonymous poster said, int (clsButton::*ButtFunc)(void) is correct.

When assigning to a function pointer:


typedef int (clsButton::*ButtFuncPtr)(void);
ButtFuncPtr p = &clsButton::ButtFunc;


You must use the address of operator (as far as I know). This is different than assigning to a straight C function.

Now you can either call the pointer like (object->*p). You need an instance of the object to call a pointer on it. So you need an instance of clsButton to use the member function pointer.


typedef int (clsButton::*pButtFunc)(void);
pButtFunc pmfn = &clsButton::ButtFunc;

void SomeFunction(clsButton* b)
{
(b->*pmfn)();
}


I find very little use for this technique but I know it is used by STL mem_fun(). This technique is pretty cool for calling routines by name (as listed on pg. 420 of The C++ Programming Language - Special Edition).

Null and Void,

you don''t have to point to a static member function. Static member functions don''t require the class-scope resolution (pg 420 of The C++ Programming Language - Special Edition):


class Task
{
public:
// ...
static void schedule();
};

void (*p)() = &Task::schedule; // ok
void (Task::*p)() = &Task::schedule; // error


Hope this helps,



Dire Wolf
www.digitalfiends.com

Share this post


Link to post
Share on other sites

  • 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!