gpt4 book ai didi

c++ - 返回函数本地的 std::string 的最佳方法

转载 作者:IT老高 更新时间:2023-10-28 14:00:27 25 4
gpt4 key购买 nike

在 C++ 中,从函数返回函数本地 std::string 变量的最佳方法是什么?

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 按值返回。

关于c++ - 返回函数本地的 std::string 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3976935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com