class ThreadWorker
{
public:
ThreadWorker(void);
virtual ~ThreadWorker(void);
static void DoSomething();
};
int main()
{
boost::thread thread1(ThreadWorker::DoSomething);
boost::thread thread2(ThreadWorker::DoSomething);
boost::thread thread3(&ThreadWorker::DoSomething);
}
我在玩 Boost.Thread,我注意到在将静态成员函数作为参数传递时,似乎是否使用运算符 (&) 的地址并不重要.没关系吗?如果不是,为什么?一种方法比另一种方法更正确吗?
最佳答案
这实际上并不重要。函数(自由函数和静态成员函数,而非非静态成员函数)衰减为函数指针。没有比另一种更正确的方法了,不过我碰巧更喜欢明确的方法。
C++11 标准,4.3/1:
An lvalue of function type T can be converted to a prvalue of type “pointer to T.” The result is a pointer to the function.
C++11 标准,5.2.2/1 - 函数调用:
There are two kinds of function call: ordinary function call and member function call. A static member function is an ordinary function.
关于c++ - 将静态方法作为参数传递,不需要地址运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7761527/