Skip to main content
GameDev.net gamedev.net
🔒 Locked

[c++] Passing a function ptr to a member function

Started by VanillaSnake21 Sep 19, 2009 at 6:57 PM 7 replies 1.4k views
Original Post
VanillaSnake21
VanillaSnake21
Hi, I'm having trouble finding info on how to pass a function as a pointer to a member function, this example will explain it more clearly:

class Foo
{
public:

void SetRenderCallback(int (*SomeFunc)() )
{
    m_Callback = SomeFunc;
}

void Render()
{
   //call the callback
   m_Callback();
}

private:

//somehow declaring a pointer to a function;
int (*m_Callback) ();
};

// another file
Foo FooClass;
int MyCallback();
FooClass.SetRenderCallback(&MyCallback);
FooClass.Render();



What would be the correct way to accomplish that? Thanks.
You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts
Denzin
Denzin
whats the error your getting?
VanillaSnake21
VanillaSnake21
Quote:
Original post by Denzin
whats the error your getting?


Well actually I havent really tried to compile it because I wanted to be at least 50% sure of what I'm doing. I just threw together the above code from how I think it might look.
You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts
FReY
FReY
This should do it:

typedef int (*RenderCB)();class Foo{public:void SetRenderCallback(RenderCB renderCB ){    m_renderCB = renderCB;}void Render(){   //call the callback   m_renderCB();}private:   RenderCB m_renderCB;};// another fileFoo FooClass;int MyCallback();FooClass.SetRenderCallback(MyCallback);FooClass.Render();
do unto others... and then run like hell.
VanillaSnake21
VanillaSnake21
Thanks FReY! Everything seems to compile perfectly except for the part where I pass my function as the pointer:

Foo class;
class.SetCallback(&MyClass::MyCallback);

I forgot to mention that I will be calling the SetCallback from another class, so technically MyCallback is a member function. How would I pass it? Right now I'm getting this error:

Foo::SetRenderCallback' : cannot convert parameter 1 from 'int (__thiscall MyClass::* )(void)' to 'RenderCallback'

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts
FReY
FReY
Don't use &MyClass::MyCallback, ie. just do this:

Foo class;class.SetCallback(MyClass::MyCallback);


Also, you'll need to make sure MyClass::MyCallback is a static class function.

ie.
class MyClass
{
static int MyCallback();
};
do unto others... and then run like hell.
nobodynews
nobodynews
Read this for more information on why you can't use function pointers with member functions as well as ways around this limitation.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
VanillaSnake21
VanillaSnake21
Thanks guys, but while I was waiting for a response I managed to think my way around using a function ptr. I came up with this:

class CallbackFunction{public:~virtual CallbackFunction() {}virtual Function() = 0;};class SuperClass{private: class MyCallback : public CallbackFunction {   Function() { ... } };}m_RenderCallback;


Then I just pass the m_RenderCallback to a function.

Thanks everyone :)
You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts
Zahlman
Zahlman
Yes, that is a good technique. But in C++, don't use a named member function for this; instead, overload the operator(). That allows you to treat the variable name for the instance as a "function name".

More here, especially near the end.

Topic Locked

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

Sign in to reply to this topic.