两个数组下标重载读写 [英] Two array subscript overloading to read and write

查看:106
本文介绍了两个数组下标重载读写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个类有两个数组下标运算符重载:一个用于读取,另一个用于写入。



浇筑是保留计数器的变化。我阅读了(在 http://faculty.cs.niu.edu/ 〜mcmahon / CS241 / c241man / node97.html ),我可以这样做:

  template< typename T> 
class Array
{
public:
Array()
{
data = new T [100];
}

T& operator [](int index)
{
cout< 正在写\\ n;
changing ++;
return data [index];
}

T operator [](int index)const
{
cout< 正在阅读\\\
;
return data [index];
}

private:
T * data;
int changes;
};

但这在我的情况下不起作用。我使用g ++ 4.7与-std = c ++ 11,实际上只有写作打印在屏幕上,即使我这样做:

  Array< int>一个; 
a [0] = 3;
cout<< a [0]< endl

我也注意到,后者从来没有通过使用gcov检查源来调用。
该页面上的方法是否完全错误,或者是我误解了?



提前感谢。

 模板< typename T> 
class CountingProxy
{
public:
CountingProxy(int& counter,T& ref):counter_(counter),ref_(ref)
{
}

CountingProxy< T> operator =(const T& o)
{
cout< 正在写\\ n;
counter _ ++;
ref_ = o;
return * this;
}

operator T()
{
cout< 正在阅读\\\
;
return ref_;
}

private:
int&计数器_;
T& ref_;
};

template< typename T>
class Array
{
public:
Array()
{
data = new T [100];
}

CountingProxy< T> operator [](int index)
{
return CountingProxy< T>(changes,data [index]);
}

T operator [](int index)const
{
cout< 正在阅读\\\
;
return data [index];
}

private:
T * data;
int changes;
};

另一方面,你可能会通过实现单独的函数读取和写入元素您的数组,例如 T& get(int index) const T& get(int index)const void put(int index,const T& value)


I'd like to have a class with two array subscript operator overloads: one used for reading and the other for writing.

The pourpose is to keep a counter of changes. I read (at http://faculty.cs.niu.edu/~mcmahon/CS241/c241man/node97.html) that I could do something like this:

template<typename T>
class Array
{
public:
    Array()
    {
        data = new T[100];
    }

    T &operator[] (int index)
    {
        cout << "Is writing\n";
        changes++;
        return data[index];
    }

    T operator[] (int index) const
    {
        cout << "Is reading\n";
        return data[index];
    }

private:
    T *data;
    int changes;
};

But that doesn't work in my case. I'm using g++ 4.7 with -std=c++11 and actually only "Is writing" is printed on screen, even if I do:

Array<int> a;
a[0] = 3;
cout << a[0] << endl;

I also noticed that the latter is never called by inspecting the source with gcov. Is the method on that page completely wrong, or is something I'm misinterpreting?

Thanks in advance.

解决方案

This is only a rough idea and I haven't thought about all the implications, but a proxy class with overloaded assignment and cast operators could solve your problem:

template<typename T>
class CountingProxy
{
public:
    CountingProxy(int& counter, T& ref) : counter_(counter), ref_(ref)
    {
    }

    CountingProxy<T>& operator=(const T& o)
    {
        cout << "Is writing\n";
        counter_++;
        ref_ = o;
        return *this;
    }

    operator T()
    {
        cout << "Is reading\n";
        return ref_;
    }

private:
    int& counter_;
    T& ref_;
};

template<typename T>
class Array
{
public:
    Array()
    {
        data = new T[100];
    }

    CountingProxy<T> operator[] (int index)
    {        
        return CountingProxy<T>(changes, data[index]);
    }

    T operator[] (int index) const
    {
        cout << "Is reading\n";
        return data[index];
    }

private:
    T *data;
    int changes;
};

On the other hand, you probably would be better off by implementing separate functions for reading and writing elements of your array, such as T& get(int index), const T& get(int index) const and void put(int index, const T& value).

这篇关于两个数组下标重载读写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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