best way to return an std::string that local to a function

前端 未结 6 650
陌清茗
陌清茗 2021-01-30 16:07

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\");
    ret         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-30 16:48

    Well, ret will have a value of mystring after MyFunc(). In case of returning the result by value a temporary object is constructed by copying the local one.

    As for me, there are some interesting details about the topic in these sections of C++ FAQ Lite.

提交回复
热议问题