c++ - Returning char array as std:string -


Is the following safe, such as without a clear cast or call the std :: string constructor? If not safe, why not?

  std: string myfunc () {char buf [128] = ""; // Put some in the buff or not the basis on the logic. Return buff; }    

Yes it's absolutely right, the caller will get a copy of the local buffer because std :: string will make a deep copy from this local buffer!

Edit: I buff is a redundant string!

Comments