c++ - 将静态方法作为参数传递,不需要地址运算符?

标签 c++ function-pointers static-methods member-functions address-operator

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/

相关文章:

c - 如何将条件作为参数传递给 C 中的函数?

C++编程之谜,有趣的函数指针

c++ - 如何在运行时恢复函数指针的类型

c++ - log4cxx 配置文件语法

c++ - 在图像中的一些点周围绘制边界

C++11:如何访问不同类型的 std::functions 的目标?

c++ - 如何从私有(private)基类调用静态方法?

c# - 典型 3 层业务层中的静态方法与实例方法

java - 避免 JavaFX 中跨类的静态

c++ - 在不同语言的程序之间共享变量的事实标准是什么?