返回函数本地的std :: string的最佳方法 [英] best way to return an std::string that local to a function
本文介绍了返回函数本地的std :: string的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C ++中,从函数返回函数本地std :: string变量的最佳方法是什么?
In C++ what is the best way to return a function local std::string variable from the function?
std::string MyFunc()
{
std::string mystring("test");
return mystring;
}
std::string ret = MyFunc(); // ret has no value because mystring has already gone out of scope...???
推荐答案
否.那是不对的.即使 mystring
已超出范围并被销毁, ret
仍具有mystring的副本,因为函数 MyFunc
按值返回.
No. That is not true. Even if mystring
has gone out of scope and is destroyed, ret
has a copy of mystring as the function MyFunc
returns by value.
这篇关于返回函数本地的std :: string的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文