将静态方法作为参数传递,不需要地址运算符?
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,我注意到在将静态成员函数作为参数传递时是否使用运算符 (&) 的地址似乎并不重要。没关系吗?如果没有,为什么?一种方法比另一种方法更正确吗?
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);
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上并不重要。函数(自由函数和静态成员函数,而不是非静态成员函数)衰减为函数指针。没有一种方法比另一种更正确,但我碰巧更喜欢明确的方法。
C++11 标准,4.3/1:
C++11 标准,5.2.2/1 - 函数调用:
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 Standard, 4.3/1:
C++11 Standard, 5.2.2/1 - Function call: