右值引用的注意事项

不要将函数的返回值设为&&,

std::string &&get(){
     std::string s = "str";return std::move(s); 
}

代码中返回的字符串s在函数返回时就已经离开作用域调用析构函数,函数的得到的返回值其实是一个野指针。

 

猜你喜欢

转载自www.cnblogs.com/HadesBlog/p/12821627.html