为什么std :: string的const访问器返回引用?

埃姆莱

std::string存取器(backfrontat,和operator[])具有const和非const过载,如下:

char& x();
const char& x() const;

为什么第二个版本会返回const引用,而不是简单地char 按值(作为副本)返回?

根据有关如何传递对象的经验法则,当不需要修改原始对象时,是否应该按值传递小对象?

本杰明·林德利(Benjamin Lindley)

因为调用者可能想要引用char,所以它反映了通过非const途径对其所做的任何更改。

std::string str = "hello";
char const& front_ref = static_cast<std::string const&>(str).front();
str[0] = 'x';
std::cout << front_ref; // prints x

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么要为基本类型返回const引用?(std :: string :: operator [])

来自分类Dev

为什么std :: string str = {}?

来自分类Dev

为什么std :: declval添加引用?

来自分类Dev

为什么std :: max由const&返回?

来自分类Dev

为什么`std :: string`的赋值运算符将`char`按值而不是`const`引用?

来自分类Dev

为什么std :: max返回错误的值?

来自分类Dev

为什么返回std :: vector仍在复制?

来自分类Dev

为什么std :: sort不按引用接受比较器?

来自分类Dev

为什么std :: string_view比const char *快?

来自分类Dev

为什么std :: cbegin返回与std :: begin相同的类型

来自分类Dev

为什么没有std :: to_string的std :: string重载

来自分类Dev

为什么std :: array :: begin不返回迭代器?

来自分类Dev

为什么std :: string不是std :: vector的特化?

来自分类Dev

为什么我需要写“ std :: string”而不是“ std :: getline()”?

来自分类Dev

为什么用“ std :: vector <std :: string>&”代替“ void”?

来自分类Dev

为什么std :: vector <char>比std :: string快?

来自分类Dev

为什么用“ std :: vector <std :: string>&”代替“ void”?

来自分类Dev

为什么const方法不能返回非const引用?

来自分类Dev

为什么const方法不能返回非const引用?

来自分类Dev

为什么`std :: pair`将`std :: tuple`作为ctor参数类型而不是`const std :: tuple&`?

来自分类Dev

为什么返回和非返回的std :: functions是可转换的?

来自分类Dev

为什么非const std :: array :: operator []不是constexpr?

来自分类Dev

为什么不能使用const std :: random_device?

来自分类Dev

为什么std :: set似乎强制使用const_iterator?

来自分类Dev

std :: add_const,为什么实现可作为参考?

来自分类Dev

为什么std :: move具有向前引用?

来自分类Dev

为什么返回引用时不能使用const?

来自分类Dev

为什么返回引用时不能使用const?

来自分类Dev

为什么从std :: packaged_task和std :: async返回的std :: future不同?

Related 相关文章

  1. 1

    为什么要为基本类型返回const引用?(std :: string :: operator [])

  2. 2

    为什么std :: string str = {}?

  3. 3

    为什么std :: declval添加引用?

  4. 4

    为什么std :: max由const&返回?

  5. 5

    为什么`std :: string`的赋值运算符将`char`按值而不是`const`引用?

  6. 6

    为什么std :: max返回错误的值?

  7. 7

    为什么返回std :: vector仍在复制?

  8. 8

    为什么std :: sort不按引用接受比较器?

  9. 9

    为什么std :: string_view比const char *快?

  10. 10

    为什么std :: cbegin返回与std :: begin相同的类型

  11. 11

    为什么没有std :: to_string的std :: string重载

  12. 12

    为什么std :: array :: begin不返回迭代器?

  13. 13

    为什么std :: string不是std :: vector的特化?

  14. 14

    为什么我需要写“ std :: string”而不是“ std :: getline()”?

  15. 15

    为什么用“ std :: vector <std :: string>&”代替“ void”?

  16. 16

    为什么std :: vector <char>比std :: string快?

  17. 17

    为什么用“ std :: vector <std :: string>&”代替“ void”?

  18. 18

    为什么const方法不能返回非const引用?

  19. 19

    为什么const方法不能返回非const引用?

  20. 20

    为什么`std :: pair`将`std :: tuple`作为ctor参数类型而不是`const std :: tuple&`?

  21. 21

    为什么返回和非返回的std :: functions是可转换的?

  22. 22

    为什么非const std :: array :: operator []不是constexpr?

  23. 23

    为什么不能使用const std :: random_device?

  24. 24

    为什么std :: set似乎强制使用const_iterator?

  25. 25

    std :: add_const,为什么实现可作为参考?

  26. 26

    为什么std :: move具有向前引用?

  27. 27

    为什么返回引用时不能使用const?

  28. 28

    为什么返回引用时不能使用const?

  29. 29

    为什么从std :: packaged_task和std :: async返回的std :: future不同?

热门标签

归档