c++ - Return a const reference or a copy in a getter function? -
What's better as default , a copy (1) or reference (2) one Cathedral ceremony?
square foo {public: std :: string str () {// (1) return str_; } Const std :: string & amp; Str () {// (2) return str_; } Private: std :: string str_; };
I know 2) can be sharp, but (n) is not due to RVO 1) It is safe with dangerous references, but the object is probably alive or never referenced. Is not stored.
When you write a class and you do not know (yet) what is the issue of performance and lifelong problems?
Additional questions: Does the game change when there is no plain string, but a vector?
Comments
Post a Comment