2015-02-07 136 views
2

,我写我的代码的简化版本,来说明我的问题的说法。在我的程序中,很多其他的东西都在代码中,这就是为什么我需要在一个类中有一些功能。我不知道如何定义参数,以便最终调用foo的f2。类函数指针作为一个静态函数

我可以有F0的TestClass内,如果它是解决问题的唯一途径,但我不知道怎么写,要么。

#include <iostream> 
using namespace std; 

class testClass { 
public: 
    void f1(); 
    void f2(int i); 

} foo; 

void f0(void (*f)(int)) { 
    (*f)(1337); // f = a pointer to f2, which is sent from f1 
} 

void testClass::f2(int i) { 
    cout << i; 
} 

void testClass::f1() { 
    f0(&f2); 
} 

int i; 
int main() { 
    foo.f1(); // I want foo.f1 to call f0 which should call foo.f2() 
    cin >> i; 
} 

如果我要删除testClass ::和foo,它将起作用。但由于我不能这样做,我应该如何在f0中正确定义参数?我应该如何正确调用f1内部的f0?

请帮忙!

+0

感谢您的所有支持(随时添加更多),我现在要使用Mats解决方案,因为我没有了解其他解决方案中所有功能的细节。但我不知道哪一个是最便宜的(我正在写一个国际象棋引擎),所以如果你有关于这个问题的答案,请在这里告诉我。:) – user1066278 2015-02-07 22:47:40

+0

附注:声明他们使用的变量,肯定不使用全局变量没有明显原因 – sehe 2015-02-07 22:50:38

+0

我使用递归函数的巨大树,你是否建议我仍然应该使用临时定义的变量或可能全局变量执行更好? – user1066278 2015-02-07 23:15:51

回答

1

使用std ::功能对于这一点,或模板的一切,并使用std::bind

http://en.cppreference.com/w/cpp/utility/functional/function

您需要bind反正到成员函数反正适应void(int)签名:如果你决定你想有更多的表现

Live On Coliru

#include <iostream> 
#include <functional> 
using namespace std; 

typedef std::function<void(int)> F; 

class testClass { 
public: 
    void f1(); 
    void f2(int i); 

} foo; 

void f0(F f) { 
    (f)(1337); // f = a pointer to f2, which is sent from f1 
} 

void testClass::f2(int i) { 
    cout << i; 
} 

void testClass::f1() { 
    f0(std::bind(&testClass::f2, this, std::placeholders::_1)); 
} 

int main() { 
    foo.f1(); // I want foo.f1 to call f0 which should call foo.f1() 
    int i; 
    cin >> i; 
} 

,并且不介意暗示头部实现的函数:

Live On Coliru

template <typename F> 
void f0(F f) { 
    (f)(1337); // f = a pointer to f2, which is sent from f1 
} 
+0

在模板方法中添加提示(** Live On Coliru)(http://coliru.stacked-crooked.com/a/42306c4c0f6fab4d)**) – sehe 2015-02-07 22:05:40

1

非静态成员函数需要被调用在“此”对象,它们不能被称为像顶层,或静态成员,职能。因此,您需要f0的额外参数,并调整您的语法以适应成员函数调用。

void f0(testClass *obj, void (testClass::*f)(int)) { 
    (obj->*f)(1337); 
} 

变化f1也通过this

void testClass::f1() { 
    f0(this, &testClass::f2); 
} 
+0

如果我要将f0放入testClass中,是否可以避免这种情况问题呢? – user1066278 2015-02-07 22:04:12

+0

如果你把'f0'放到你的testClass中,那么你不再需要传递'this'。 – Mat 2015-02-07 22:05:35

+0

很酷,它的工作:) thx – user1066278 2015-02-07 22:16:32

1

首先,在这里,看来你一路走下去只是冗长说所有的

returnValue someClass::f1{ 
f2(1337) 
} 

第二,如果你仍然需要(出于某种原因)来实现这种方式,我建议使用模板:

template <Class T> 
void f0 (T pointerToTFunction){ 
pointerToTFunction(1335)l 
} 

class testClass{ 

void testClass::f2(int i) { 
    cout << i; 
} 

void testClass::f1() { 
    f0<void(*testClass::A)(int)>(&testClass::f2); 
} 

} 
1

C++ 11,模板和lambdas。

#include <iostream> 
using namespace std; 

class testClass { 
public: 
    void f1(); 
    void f2(int i); 
} foo; 

template <class F> 
void f0(F&& f) 
{ 
    f(1337); 
} 

void testClass::f2(int i) { 
    cout << i; 
} 

void testClass::f1() { 
    f0([&](int x){f2(x);}); 
} 

int i = 1; 
int main() { 
    foo.f1(); 
} 
相关问题