我有下一个代码:
#include <iostream>
#include <Windows.h>
#include <thread>
#include <string>
void func(std::string& str)
{
std::string to_change("Bye...");
std::cout << "From func: " << str << std::endl;
str.swap(to_change);
}
int main()
{
std::string Meeting = "Hi!";
std::thread t1(func, Meeting);
t1.join();
std::cout << "From main: " << Meeting << std::endl;
Sleep(20000);
return 1;
}
输出结果为:
来自func:HI
来自main:HI
但如果我通过引用进行此类移动:
std::ref(Meeting)
然后输出结果:
来自func:HI
从主要:再见......
发送两种引用类型有什么区别?