我有两节课。
class first
{
public:
int sum (int a, int b)
{
return a+b;
}
};
class second
{
private:
std::thread t1, t2;
int sum (int a, int b, int c)
{
return a+b+c;
}
public:
void execute()
{
t1 = std::thread (&second::sum, this, 10,20,30); //calling same class function
t2 = std::thread (&first::sum, /*what will be here*/, 10,20); // trying to call another class function
}
};
int main()
{
second s;
s.execute();
return 0;
}
我需要通过什么代替/ *将会在这里* /
答案 0 :(得分:4)
你需要一个first
的实例(或指向first
实例的指针,它至少和线程一样长):
传递实例:
first f;
t2 = std::thread (&first::sum, f, 10,20);
传递指针:
// assume m_f is a data member of type first
t2 = std::thread (&first::sum, &m_f, 10,20);
选择哪一个取决于所需的语义。在这种情况下,first::sum
成为非静态成员函数(或者根本就是成员函数)是没有意义的。
如果您选择first::sum
成为static
成员,那么您将不需要first
的实例:
t2 = std::thread (&first::sum, 10, 20);