Returning a const reference to an object instead of a copy

前端 未结 12 599
天命终不由人
天命终不由人 2020-11-29 16:40

Whilst refactoring some code I came across some getter methods that returns a std::string. Something like this for example:

class foo
{
private:
    std::st         


        
12条回答
  •  一整个雨季
    2020-11-29 17:33

    Odds are pretty good that typical usage of that function won't break if you change to a const reference.

    If all of the code calling that function is under your control, just make the change and see if the compiler complains.

提交回复
热议问题