Pointer to a function in a class
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?
The member function needs to be static.
[Resist Windows XP''s Invasive Production Activation Technology!]
[Resist Windows XP''s Invasive Production Activation Technology!]
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
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
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:
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.
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):
Hope this helps,
Dire Wolf
www.digitalfiends.com
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; // okvoid (Task::*p)() = &Task::schedule; // error
Hope this helps,
Dire Wolf
www.digitalfiends.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement