Archived

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

Sand_Hawk

Function pointers as function arguments

Recommended Posts

How can I use function pointers in an function call? I need to give the address of some function to one of my classes. Thnx, sand Hawk Member of the Stupid Coders. http://www.stupidcoders.cjb.net -Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites
  
class CClass{
protected:
char *(*pointer_to_the_function)(HWND,int,long);//any parametrs and return types

public:
void SetFunctionPointer(char *(*some_name)(HWND,int,long));
}

void CClass::SetFunctionPointer(char *(*some_name)(HWND,int,long))
{
//assigns the pointer given as a parametr to the pointer

//we have declared as a member variable

pointer_to_the_function=some_name;
}



Hope it hepls. Of course, the parametrs and return values could vary

Share this post


Link to post
Share on other sites
this "should" work:

  

//MyClass.h

class MyClass
{
protected:
void (*SomeFuncPtr)();
void (*SomeOtherFuncPtr)(int i, int j);

public:
void SetSomeFuncPtr(void (*ptr)()) { SomeFuncPtr = ptr; }
void SetSomeOtherFuncPtr(void (*ptr)(int i, int j)) { SomeOtherFuncPtr = ptr; }
void CallSFPtr() { SomeFuncPtr(); }
void CallSOFPtr(int i, int j) { SomeOtherFuncPtr(i, j); }
};

//main.cpp


#include "MyClass.h"

void MyFunc()
{
//do something

}

int MyFunc2(int i, int j)
{
//do something

return 1;
}

void main()
{
MyClass mc;
mc.SetSomeFuncPtr(MyFunc);
mc.SetSomeOtherFuncPtr(MyFunc2);
mc.CallSFPtr();
mc.CallSOFPtr();
}


I hope this helps

Share this post


Link to post
Share on other sites
NOTE: when passing function pointers as parameters i suggest you specify the call type.

i.e.

__cdecl
__stdcall
__thiscall
__fastcall

this will save you headache in the long run....

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
And how would I do this with my WinProc(The function to handle the Windows Events)? I tried this:

    
class CWindowClass
{
...
LRESULT *(*WinProc) (HWND Hwnd, UINT Msg, WPARAM wParam, LPARAM lParam);
};

//In source to assign it to the WinClass(WNDCLASSEX type):


WinClass.lpfnWndProc = WinProc;


Error: WINDOWCLASS.H(48,27):Cannot convert 'long * (*)(HWND__ *,unsigned int,unsigned int,long)' to 'long (__stdcall *)(HWND__ *,unsigned int,unsigned int,long)'

What's the problem with this code?

Sand Hawk

Member of the Stupid Coders.
http://www.stupidcoders.cjb.net

-Earth is 98% full. Please delete anybody you can.

Edited by - sand_hawk on February 21, 2002 4:09:06 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Your return type shouldn''t be a pointer to a long.

Share this post


Link to post
Share on other sites
exactly what i was talking about. j/k.

but anyway, there error message is actually pretty informative.


    
class SomeClass {
protected:
// variable declaration.

LRESULT (CALLBACK *SomeFuncPtr) (HWND Hwnd, UINT Msg, WPARAM wParam, LPARAM lParam);
public:
// method declaration.

void SetFuncPtr(LRESULT (CALLBACK *WinProc)(HWND Hwnd, UINT Msg, WPARAM wParam, LPARAM lParam));
};



Edit: the default calling convention is "__cdecl" and Windows procedures are "CALLBACK --> __stdcall".


To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Edited by - jenova on February 21, 2002 4:25:52 PM

Share this post


Link to post
Share on other sites