This question already has answers here:
How do I specify a pointer to an overloaded function?

(6 个回答)


7年前关闭。



#include <thread>
struct Callable
{
    void start() {};
    void start(unsigned) {};
};

int main()
{
    Callable some_object;
    std::thread some_thread( &Callable::start, &some_object );

    some_thread.join();
}

此代码无法编译,因为 &Callable::start 不明确。有没有办法指定应该在 std::thread 构造函数中使用哪个重载?

最佳答案

你可以转换:

using callback_type = void (Callable::*)();

std::thread some_thread(
    static_cast<callback_type>(&Callable::start), &some_object );
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

关于c++ - 如何指定可调用对象应指向的重载?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21816984/

10-12 19:38