c++ - What is better: return std::string by value or by constant reference? -


this question has answer here:

here class 2 getters different return type:

class {     std::string m_test { "test" };  public:     std::string test_by_value { return m_test; }     const std::string& test_by_const_ref() { return m_test; } };  // ... 

which better? std::string, not built-in types. s.t.l. says in https://channel9.msdn.com/events/goingnative/2013/don-t-help-the-compiler better return value, because multiple copies optimized? or incorrectly understanded him?

by value.

i encountered similar code in wild:

a foo();  std::string const& x = foo().test_by_const_ref(); 

boom, x dangling reference.

that not happen returns value.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -