是否可以对我们需要使用线程跨越的函数进行重载?
我有一个名为 Complex 的简单类。
class Complex
{
public:
Complex():realPart_(0), imagPart_(0){}
Complex(double rp, double ip) : realPart_(rp), imagPart_(ip) {}
double & real() { return realPart_;}
double & imag() { return imagPart_;}
const double & real() const { return realPart_;}
const double & imag() const { return imagPart_;}
double square() const {return realPart_*realPart_ - imagPart_*imagPart_;}
void display() const
{
std::cout << "Square of the Complex number (" << realPart_ << ") + i (" << imagPart_ << " ) is " << square() << std::endl;
}
void display(unsigned nTimes) const {while(nTimes-- > 0)display();}
private:
double realPart_;
double imagPart_;
};
void Test3()
{
Complex c1(1, 0.74), c2(2, 0.35);
std::thread sqCalc1(&Complex::display, &c1);
std::thread sqCalc2(&Complex::display, &c2);
sqCalc1.join();
sqCalc2.join();
}
构建此代码时出现错误。
error C2661: 'std::thread::thread' : no overloaded function takes 2 arguments
如果没有采用无符号的重载显示函数,那么我显示的代码可以正常工作。