#include <iostream>
#include <functional>
using namespace std;
//typedef struct {
// int (*add)(int a, int b);
//} GFUNC_T;
typedef std::function<int(int a, int b)> GFUNC_T;
GFUNC_T gfun;
class Test
{
public:
Test();
public:
int add(int a, int b);
int (*padd_func)(int a, int b);
};
int (Test::* padd_func)(int, int) = &Test::add;
Test::Test()
{
// register function pointer to gfun
//gfun.add = padd_func;
auto lambdaWrapper = [this](int a, int b){
return this->add(a, b);
};
gfun = lambdaWrapper;
cout << "hello\n";
}
int Test::add(int a, int b)
{
return a+b;
}
int main()
{
Test mytest;
cout << mytest.add(23, 44) << endl;
if (gfun != NULL)
{
cout << gfun(10,20);
}
return 0;
}
【 在 lobachevsky 的大作中提到: 】
: 我试了一下,简化成了下面的样子(当然比你的例子复杂一点).
: 本质上,我需要在gfunc(实际上在另外一个文件里面)里面去调用执行myclass里面的函数指针.所以写得有点啰嗦:
: #include <iostream>
: ...................
--
修改:DoorWay FROM 38.75.136.*
FROM 38.75.136.*