2010-07-23 47 views
3

我使用boost(信号+绑定)和C++来传递函数引用。下面是代码:绑定函数问题

#define CONNECT(FunctionPointer) \ 
     connect(bind(FunctionPointer, this, _1)); 

我用这个像这样:

class SomeClass { 
    void test1() {} 
    void test2(int someArg) {} 

    SomeClass() { 
    CONNECT(&SomeClass::test1); 
    CONNECT(&SomeClass::test2); 
    } 
}; 

二测功能结合作品(测试2),因为它至少有一个参数。第一次测试我有一个错误:

‘void (SomeClass::*)()’ is not a class, struct, or union type 

为什么我不能没有参数传递函数?

回答

4

_1是一个占位符参数,意思是“用第一个输入参数替换”。方法test1没有参数。

创建两个不同的宏:

#define CONNECT1(FunctionPointer) connect(bind(FunctionPointer, this, _1)); 
#define CONNECT0(FunctionPointer) connect(bind(FunctionPointer, this)); 

但要记住macros are evil

而且使用这样的:

class SomeClass { 
    void test1() {} 
    void test2(int someArg) {} 

    SomeClass() { 
    CONNECT1(&SomeClass::test1); 
    CONNECT0(&SomeClass::test2); 
    } 
}; 
+0

好吧,我udnerstand。我知道宏是邪恶的,但我的宏的身体是巨大而丑陋的。当然,如果它像我的样本,我会用它。谢谢。 – Ockonal 2010-07-23 11:29:45