将静态方法作为参数传递,是否不需要地址运算符? [英] Passing static method as argument, no address-of operator required?
问题描述
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,我注意到在传递静态成员函数作为参数时,是否使用运算符(&)的地址并不重要.争论.没关系吗如果没有,为什么呢?一种方法比另一种更正确吗?
I'm playing around with Boost.Thread and I notice it doesn't seem to matter whether I use the address of operator (&) or not when passing a static member function as an argument. Does it not matter? And if not, why? Is one way more correct than the other?
推荐答案
实际上没关系.函数(自由函数和静态成员函数,而非非静态成员函数)衰减为函数指针.没有其他方法比其他方法更正确,不过我碰巧更喜欢显式方法.
It effectively does not matter. Functions (free functions and static member functions, not non-static member functions) decay to function pointers. No way is more correct than the other, I happen to prefer the explicit one though.
C ++ 11标准,4.3/1:
函数类型T的左值可以转换为指向T的指针"的前值.结果是指向该函数的指针.
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 Standard,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.
这篇关于将静态方法作为参数传递,是否不需要地址运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!