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

后端 未结 7 1084
刺人心
刺人心 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:37

    Rule #1 of optimization: Measure, optimize, measure. Or, as Knuth said, "premature optimization is the root of all evil".

    Unless you have a strong indication that simply returning std::string will significantly impact the performance of your software, just do so. If you can measure a significant impact, find the critical path, and optimize that. Don't make any funny, project-wide "optimizations" that likely result in little to no performance benefit, but negatively impact the readability, maintainability and robustness of your code.

提交回复
热议问题