的std::string
访问者(back
,front
,at
,和operator[]
)具有const
和非const
过载,如下:为什么std :: string的const访问器返回一个引用?
char& x();
const char& x() const;
为什么第二版本返回const
参考,而不是简单地返回char
价值(作为副本)?
根据rules of thumb on how to pass objects around,当不需要修改原始值时,不应该通过值传递小对象吗?
一个字:一致性(例如'std :: vector')。 – Simon
为什么它应该与非const版本有不同的语义? – juanchopanza