【问题标题】:Difference between Operator [] overloading write/read?运算符 [] 重载写入/读取之间的区别?
【发布时间】:2014-12-14 13:55:49
【问题描述】:

我是 C++ 新手,因为这个问题我很抱歉,但这是一场斗争。如果有人可以帮助我区分以下几行,我将不胜感激。

char& operator [](int);         // write (why with reference?)
char operator [](int) const;    //read (why without a reference?)
char const &operator[](int) const; // what is the difference compared to the previous line?
const char *& operator[] (const int* ); // is this also possible?

【问题讨论】:

    标签: c++ pointers reference operator-overloading


    【解决方案1】:

    您可能想阅读Operator overloading 的概述。

    所以,回顾一些适用的观点:

    • operator[]始终非静态一元成员函数。
    • 可以有多个重载,就像任何其他成员函数一样。
    • 按照惯例(注意!),非 const 限定版本会返回一个用于修改内容的引用(该引用可能由代理类型表示,但尽量避免这样做)。
    • 按照惯例,const 限定的重载可以返回 const 引用或副本,以提供对相同元素的只读访问。
      • 如果元素不能动态生成,尤其是复制成本不低且不便宜时,请使用const-reference。
      • 如果上述不成立,则使用 value-return。

    顺便说一句:您真的希望const 和非const 成员相似,因此非const 可以是一个简单的内联函数,通过适当的const_cast 委托给另一个。 (不要反其道而行之,那样是不被允许的,也不安全的。)

    关于您的最后一行,它使用指向 const int 的指针进行索引,并返回对指向 const char 的指针的引用。
    这是一个非常奇怪的返回值和索引,但如果你有一个有效的用途,为什么不呢?

    【讨论】:

      【解决方案2】:

      为了确保通过一些代码得到答案,以下是这些版本的使用方式的一些示例:

      // Q: char& operator [](int);         // write (why with reference?)
      // A: this is using [] to return a mutable reference to a conceptual element in my array
      my_type x; // x supports operator []
      x[6] = 'A'; // actually modifies the 7th element of x
      

      -

      // Q: char operator [](int) const;    //read (why without a reference?)
      // A: this will return a COPY of the element at [i].
      //    for a char this is irrelevant, a copy is trivial
      //    if it was some large object you might want to return 
      //    a const& instead and avoid the copy
      char my_copy = x[6];
      x[6] = 'B'; // from above
      // now my_copy is 'A' but x[6] is 'B'
      

      -

      // Q: char const &operator[](int) const; // what is the difference compared to the previous line?
      // A: as mentioned, for a char not a lot of difference. 
      //    For a large object it avoids a copy
      

      -

      // Q: const char *& operator[] (const int* ); // is this also possible?
      // A: yes it's possible. Yes it's completely evil. No, don't do it.
      

      【讨论】:

        【解决方案3】:

        当您像char& operator [](int) 一样重载运算符[] 时,它可以读取/写入由条目整数索引的底层char

        但是在许多情况下,您需要读取 const 对象的属性,然后您必须通过 const 方法重载该运算符,例如 char operator [](int) const

        第三个char const &operator[](int) const和上面一样有用,但是当底层变量是const时。

        【讨论】:

        • 感谢您的回答。你说“当你重载运算符 [] 像 char& 运算符 [](int) 时,它可以读/写由条目整数索引的底层 char。”如果最后有 const 有什么区别:char& operator [](int) const。仍然可以读写。不是吗?如果我通过 const 方法重载运算符,我看不到发生了什么变化,这似乎与方法不是 const 相同。
        • 通过将const放在方法的末尾,方法不能改变成员(除了可变成员)。因此,您可以阅读。而且,返回char& 已经没有用了。
        • 主要:字符串字符串(“字符串”);字符串[3]='n';但这怎么可能呢?即使我使用这种方法 - char& operator [](int) const - 来重载 [],它也会编译。而且我还没有将成员声明为可变的。
        • 我确信它有一个没有const 的重载,以便允许更改字符串的字符。
        【解决方案4】:

        第一行告诉你,当你修改返回的 operator[] 的值时,旧的值也会改变。

        第二行告诉你这个操作符不能修改任何变量,但是你可以修改return的值,不要改旧的。

        第三行告诉你这个操作符不能修改任何变量或返回值。

        第二行和第三行的区别是前者可以修改return的值,后者不能修改return的值,和最后一样,我也是c++新手,不知道是什么这意味着。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-12-30
          • 1970-01-01
          • 1970-01-01
          • 2017-05-05
          • 1970-01-01
          • 1970-01-01
          • 2011-05-24
          • 2022-01-19
          相关资源
          最近更新 更多