Archived

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

Doodle

Need Help With Function Pointers!!!

Recommended Posts

Suppose I have a class CClass class CClass { private: BOOL (*fp)(BYTE n); BOOL func(BYTE n){} }; CClass::CClass { fp = func; } When I try to compile it in Visual Studio 6 i get an error: error C2440: ''='' : cannot convert from ''int (__thiscall CClass::*)(unsigned char)'' to ''int (__cdecl *)(unsigned char)'' There is no context in which this conversion is possible. Can somebody help me out. I just can''t seem to find the problem here. Thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
There is a difference between a global/static function and a member function. Therefore, a member function pointer is different from a normal function pointer:

  class CClass {
CClass();
BOOL (CClass::*fp)(BYTE n);
BOOL func(BYTE n)
{
}
};

CClass::CClass()
{
fp = func;

// call fp on this object

(this->*fp)(255);
}

Notice that you need an object to call the member function pointer on. The actual call is accomplished with the following syntax:
(obj.*mem_fn_ptr)(arguments);
// or
(p_obj->*mem_fn_ptr)(arguments);


Hope this helps.

Share this post


Link to post
Share on other sites