相关疑难解决方法(0)

访问者应该返回值还是常量引用?

假设我有一个Foostd::string成员的班级str.应该get_str返回什么?

std::string Foo::get_str() const
{
    return str;
}
Run Code Online (Sandbox Code Playgroud)

要么

const std::string& Foo::get_str() const
{
    return str;
}
Run Code Online (Sandbox Code Playgroud)

什么是C++更惯用?

c++ reference accessor return-value

16
推荐指数
3
解决办法
8640
查看次数

标签 统计

accessor ×1

c++ ×1

reference ×1

return-value ×1