What is the best way to return string in C++?

后端 未结 7 1083
刺人心
刺人心 2020-12-04 15:16

My question is simple: if I have some class Man and I want to define member function that returns man\'s name, which of the following two variants shall I prefer?

Fi

7条回答
  •  不思量自难忘°
    2020-12-04 15:39

    As we have move-semantics (in C++11), you can use this:

    string name();
    

    Even in C++03, this is almost good, as the compiler might optimize this (Search for Return Value Optimization).

提交回复
热议问题