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

Started by
10 comments, last by MarkusPfundstein 12 years, 6 months ago
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?
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?

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.

In C++ using function pointers of member functions is not allowed.

Function pointers to member functions are indeed allowed.
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.

[quote name='irreversible' timestamp='1319299606' post='4875347']
In C++ using function pointers of member functions is not allowed.

Function pointers to member functions are indeed allowed.
[/quote]

I stand corrected - misleading parts removed from my answer.

[quote name='irreversible' timestamp='1319299606' post='4875347']
In C++ using function pointers of member functions is not allowed.

Function pointers to member functions are indeed allowed.
[/quote]

I stand corrected - misleading parts removed from my answer.
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(); } );
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.

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. :)

This topic is closed to new replies.

Advertisement