Should accessors return values or constant references?

后端 未结 8 1287
谎友^
谎友^ 2020-12-05 08:17

Suppose I have a class Foo with a std::string member str. What should get_str return?

std::string Foo::ge         


        
8条回答
  •  难免孤独
    2020-12-05 08:44

    AFAIK, the rule is same as the one which is used while deciding whether to take a function parameter by value or const reference. If the sizeof the value being returned is small enough then I tend to use returning a copy else return a const reference.

提交回复
热议问题