Jump to content
  • Advertisement
Sign in to follow this  
Knight52

[C++] pass a class's function to another class

This topic is 2610 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to pass a function in class B, which inherits from class A ,to class A, to store and call when an event occurs.

my code looks like this:


class A
{
void (*action)();
public: void setAction(void function()) {action = function;}
void activate() {(*action)();}
};
class B : A
{
private: void functionIwantToPass() {...}
public: B()
{ setAction(&B::functionIwantToPass); }
};


It gives me this:

error C2664: 'A::setAction' : cannot convert parameter 1 from 'void (__thiscall B::* )(void)' to 'void (__cdecl *)(void)'

How should I put it or modify it?

Share this post


Link to post
Share on other sites
Advertisement
A global function and a member function are not the same. even if they appear to have the same signature, a non-static member function always has a hidden "this" parameter.
May I suggest using boost::function?

Share this post


Link to post
Share on other sites

How should I put it or modify it?



You can make your function static and pass an instance pointer to it to gain access to object data (also cleared up some function pointer syntax):




typedef void (*fnPtr)(class A*);


class A
{
fnPtr action;
public: void setAction(fnPtr function) {action = function; }
void activate() { fnPtr(this);}
};
class B : A
{
private:

static void functionIwantToPass(A* obj) {...} //cast obj to B* here
public: B()
{ setAction(B::functionIwantToPass); }
};



Hope this helps.

Share this post


Link to post
Share on other sites
It is a terribly classic answer, but: could you describe more precisely what you are trying to achieve ?
From your simple example, it seems that just using a virtual method would do the job, without using any function pointer.

Share this post


Link to post
Share on other sites
The following lets you accept generic functions (without explicit class types or *this* parameters). From the top of my head... (exact syntax ay be wrong)

#include <function> //or boost function
void foo(function<void ()> f) { ... }

class x
{
void y() { }
}
//...
x x_inst;

function<void ()> func = bind(&x::y, &x_inst);
foo(func);

or with lambdas. I remember you can't directly accept function types (at least under MSVC)
template<typename TFunc>
void foo2(TFunc f)
{
function<void ()> func(f);
}

x x_inst; //be careful w/ the ref in the lambda...
foo2( [&] () { x_inst.y(); } );

Share this post


Link to post
Share on other sites
So all I need to do is set that function to static. Thanks.
However, I found that I also have to set every variables used in that function to static. That leads to making class B static which leads to unresolved external symbols from static B b[40][40] in another class. I'll try another approach.


It is a terribly classic answer, but: could you describe more precisely what you are trying to achieve ?
From your simple example, it seems that just using a virtual method would do the job, without using any function pointer.


class A is a button and class B is "Mine Button" in MineSweeper.

Share this post


Link to post
Share on other sites

So all I need to do is set that function to static. Thanks.
However, I found that I also have to set every variables used in that function to static. That leads to making class B static which leads to unresolved external symbols from static B b[40][40] in another class. I'll try another approach.

[quote name='SriLumpa' timestamp='1319300932' post='4875356']
It is a terribly classic answer, but: could you describe more precisely what you are trying to achieve ?
From your simple example, it seems that just using a virtual method would do the job, without using any function pointer.


class A is a button and class B is "Mine Button" in MineSweeper.
[/quote]

Look at my earlier post and use bind(...) (from C++0x's <function> or boost/function). It essentially encapsulates a function callstate. You don't need to make anything static. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!