C ++ - 基于赋值的一侧重载[]运算符 [英] C++ - Overloading [] operators based on the side of assignment

查看:178
本文介绍了C ++ - 基于赋值的一侧重载[]运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在c ++中编写一个动态数组模板

I'm trying to write a dynamic array template in c++

我目前正在重载[]运算符,我想实现一个不同的行为

I'm currently overloading the [] operators and I'd like to implement a different behavior based on which side of assignment they are used on.

#include <iostream>
...

template <class T>
T dynamic_array<T>::operator[](int idx) {
    return this->array[idx];
}

template <class T>
T& dynamic_array<T>::operator[](int idx) {
    return this->array[idx];
}

using namespace std;
int main() {
    dynamic_array<int>* temp = new dynamic_array<int>();

    // Uses the T& type since we are explicitly 
    // trying to modify the stored object
    (*temp)[0] = 1;

    // Uses the T type since nothing in the array 
    // should be modified outside the array
    int& b = (*temp)[0]; 

    // For instance...
    b = 4;
    cout<<(*temp)[0]; // Should still be 1
    return 0;
}

如果出现明显的原因,

I get compiler errors when trying to overload like this for obvious reasons.

有正确的方法吗?

我的搜索到目前为止还不成功。任何我所看到的重载的[]操作符似乎接受用户可以修改对象外面的存储项。

My search so far has not been successful. Anything I've seen with overloaded [] operators seems to accept that the user can modify the stored item outside of the object.

我实现了使用方法(int i),update(int i,T obj)),但是能够像普通数组一样使用这个类会更好。

I've implemented methods to use (instance(int i), update(int i, T obj)) but it would be nice to be able to use this class like a regular array.

推荐答案

Scott Meyers在有效的C ++ 书中提到这个问题。基本上,诀窍是从索引操作符( operator []() operator []()中返回一个临时的const或非const代理对象)const ),然后重载该代理类的赋值和隐式转换运算符。类似这样:

Scott Meyers talked about this in one of the Effective C++ books. Basically the trick was to return a temporary const- or non-const proxy object from the index operators (operator[]() and operator[]() const), then overload the assignment and implicit conversion operators for that proxy class. Something like this:

template <class T>
class Array
{
  public:
    struct proxy {
      T& element;

      proxy(T& el) : element(el) {}

      operator const T& () const {
        return element; // For use on RHS of assignment
      }

      proxy& operator=(const T& rhs) {
        // For use on LHS of assignment
        // Add your logic here
      }
    };

    const proxy operator[](int i) const {
      return proxy(a[i]);
    }

    proxy operator[](int i) {
      return proxy(a[i]);
    }

  private:
     T* a;
};

我可能有一些细节错了,但是想法是延迟分配元素将打开,直到实际尝试分配给它。也就是说,你不知道在operator []调用时会做什么,但是当你尝试分配给后续的元素引用时,你肯定会这样做。

I may have some of the details wrong but the idea is to defer the decision of what side of the assignment the element is on until an actual attempt is made to assign to it. That is, you don't know what will be done at the time of the operator[] call, but you certainly do when you attempt to assign to the subsequent element reference.

这篇关于C ++ - 基于赋值的一侧重载[]运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆