gpt4 book ai didi

c++ - 使用 C++0x std::thread 在 new-ed 对象中调用成员函数

转载 作者:太空宇宙 更新时间:2023-11-04 14:14:54 25 4
gpt4 key购买 nike

我想做这样的事情:

class A {
public:
void a_start () {
// somewhere in A: my_b = new B();
std::thread t(my_b->b_start); // won't compile
}

private:
B* my_b;
};

class B {
public:
void b_start();
};

但是它不会编译。我试着看看 http://accu.org/index.php/journals/1584但它没有提到成员函数位于新对象中的情况。正确的语法是什么?

最佳答案

有很多方法可以实现这一点,例如使用 std::bind 或使用 trampoline 成员函数,但我可能只使用 lambda,如下所示:

std::thread t([&](){my_b->b_start();});

关于c++ - 使用 C++0x std::thread 在 new-ed 对象中调用成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12065544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com