疑问:为什么使用的是const 引用,但是子线程中的字符串s地址与主线程中的地址不同?
void p(const string& s) {
cout <<"in thread :"<< std::this_thread::get_id()<<" s address is :"<< &s << endl;
}
int main() {
string s = "hello , world";
cout<< " s address is :"<< &s << endl;
p(s);
thread t(p, s);
t.join();
}
输出结果:
可以看到 主线程中调用函数p,输出的 s 的地址是相同的,但是调用thread的构造函数所构造的线程中s的地址与主线程不同,why?
发生了值传递