Function pointers as function arguments

Started by
5 comments, last by Sand_Hawk 22 years, 1 month ago
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.
----------------(Inspired by Pouya)
Advertisement
  class CClass{protected:   char *(*pointer_to_the_function)(HWND,int,long);//any parametrs and return typespublic:   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
this "should" work:

  //MyClass.hclass 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 somethingreturn 1;}void main(){MyClass mc;mc.SetSomeFuncPtr(MyFunc);mc.SetSomeOtherFuncPtr(MyFunc2);mc.CallSFPtr();mc.CallSOFPtr();}  


I hope this helps
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.
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.
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
----------------(Inspired by Pouya)
Your return type shouldn''t be a pointer to a long.
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
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.

This topic is closed to new replies.

Advertisement