Skip to main content
gamedev.net
🔒 Locked

Calling a function from a pointer ??

Started by Dominik_78 Dec 9, 2002 at 11:00 AM 4 replies 300+ views
Original Post
Dominik_78
Dominik_78
Hope the topic is what I am looking for... I just wonder if I can define a function that takes a function as a parameter - 1. can it be done? 2. how can I "hold" this given function in my class? 3. can this function be from an other class? example: I think of a class cButton this got the function: onAction the parameter for onAction is a function that should be called if onAction is triggered. how to make this in cpp? thank you Dominik
noVum
noVum
you can do this with boost (boost.org)


      
#include <boost/function.hpp>

#include <boost/bind.hpp>


class CButton
{
public:
template <class _Tx,class _Ty> void setAction(void (_Tx::*function)(),_Ty *this_pointer)
{
onActionFunction = boost::bind(function,this_pointer);
}
void onAction
{
onActionFunction();
}
private:
boost::function0<void> onActionFunction;
};


class X
{
void doSomething()
{
// Do something very important

}
}


int main()
{
X x;
CButton button;

button.setAction(X::doSomething,&x);
button.onAction();
}


[edited by - noVum on December 9, 2002 12:10:09 PM]

[edited by - noVum on December 9, 2002 12:12:08 PM]
CommanderXXL
CommanderXXL
1. Sure
2. Just typedef the format of your "parameter" like this

  
typedef output (__cdecl * somefunction)(Parameters);

Only the paranthesis are very important. Then you can use:


  
somefunction MyFunction;


as a pointer to your function and store your parameter in it.
Your function has to look like this


  
OnAction(somefunction Function)
{
MyFunction = Function;
}


3. Yes, but this is not that easy. You need to store the type and the implicit this pointer of the other class and the method name of the function to be called. Then you can convert it at runtime to a normal method call. This only works with a template. Cause you need to store the type of the parentclass and this is impossible at runtime.


  
template <typename ParentClass> class someclass
{
typedef void (ParentClass::*MethodProc)(void);
MethodProc m_pMethodProc;
ParentClass* m_pParentClass;
}


and now at runtime to call it:


  
someclass::myrunner()
{
reinterpret_cast<someclass*>(pv)->(m_pParentClass->*m_pMethodProc)();
}


----------------------------
By CommanderXXL
Commander@bredlmuc.de
----------------------------

[edited by - CommanderXXL on December 9, 2002 12:27:36 PM]

[edited by - CommanderXXL on December 9, 2002 12:27:59 PM]

[edited by - CommanderXXL on December 9, 2002 12:29:53 PM]
---------------------------- By CommanderXXLCommander@bloomm.de----------------------------
browny
browny
Ans 1: Ofcourse it can be done

Ans 2:

  

// this his how we define a C function

// itz similar to other typedef.

// so , from now on we will use pFuncPointer whenever we

// mean to create a function pointer of type "int xxx(int,char)"


typedef int (__cdecl *pFuncPointer)(int i,char a);

class MyMagicClass{
private:
pFuncPointer m_myMagicFunction;
public:
MyMagicClass()
{
m_myMagicFunction = NULL;
}
void setMagicFunction(pFuncPointer p)
{
m_myMagicFunction = p;
}
void executeMagicFunction(int a, chat r)
{
// check whether we are actually pointing to any

// fucntion or not

if(m_myMagicFunction)
myMyMagicFunction(a,r); // WOW !

}
};

void Magic(int i,char a)
{
printf("Hey, um inside here... with %i and %a\n",i,a);
}

void main()
{
MyMagicClass magicClass;
magicClass.setMagicFunction(Magic);
magicClass.executeMagicFunction(10,''a'');
}


Now, that''s how u pass a function pointer and execute a funcion via pointer. However, the examble above shows how to pass a C fuction [not C++] to a pointer. If u wanna pass a method [ functions of a class ] in a similar way,then u gotta make ur methods STATIC, otherwise the compiler will puke.

In order to pass NON-static method as fucntion pointer u gotta do some tricks i guess, although i''ve never tried any yet .

Ok, i''m just telling u the idea, see if it works or not.

When u compile a class with a method say "myMethod"


  
class Test{
void myMethod(int i);
};


it actually compiles the "myMethod(int i)" as "myMethod(Test *this,int i) behind the scene. WOW!, now u understand where that funny "this" pointer comes from !.

so when u define the function pointer , u just put a fake "Test*" pointer.

maybe like this

class TEST;

typdef void (*pMagicFunction)(Test *fakeThis,int i);

see it that works and lemme know
Z
noVum
noVum
Why is everybody ignoring my posts *sigh*
Dominik_78
Dominik_78
I do not ignore your post!

thank you all this helped my out!

thanks

Dominik

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.