Pass temporary object to function that takes pointer

前端 未结 4 1733
星月不相逢
星月不相逢 2020-12-18 01:27

I tried following code :

#include 
#include
using namespace std;

string f1(string s)
{
   return s=\"f1 called\";
}

void f2(s         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-18 01:46

    Try this:

    int main()
    {
        string str;
        string str2 = f1(str); // copy the temporary
        f2(&str2);
    }
    

提交回复
热议问题