使用重载下标运算符"[ ]"获取数组并将其设置为数组的值将不起作用

Getting and setting values to an array with Overloading the subscript operator “[ ]” won't work

本文关键字:数组 设置 不起作用 获取 重载 下标 运算符      更新时间:2023-10-16

在苦苦寻找答案之后…

我尝试了(u)r小时来获取和设置值到一个数组,重载下标操作符"[]",但无法弄清楚为什么它不会工作。

我在这里要做的是将someType值设置为数组成员(例如,On Main "darr1[I] = I *10.0"),重载[]和重载=,并从数组成员(On Main "<<darr1[我]& lt; & lt;endl"(例如),但不明白为什么只是重载:"Type &操作符[](int index)"正在调用。

我的程序根本没有达到'='重载或第二次'[]'重载…

下面是我的程序(很抱歉太长了):

#include <iostream>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class  AO1Array
{
private:
int _size;
protected:
int top;
int *B;
int *C;
AO1Array(int n);
~AO1Array();
bool isRealValue(int index)
{
    if ((0 <= B[index] && B[index] < top) && (index == C[B[index]]))
        return true;
    return false;
};
};
AO1Array::AO1Array(int n)
{
_size = n;
top = 0;
B = new int[n];
C = new int[n];
}

AO1Array::~AO1Array()
{
delete[] B;
B = NULL;
delete[] C;
C = NULL;
}

template<class Type>
class GenericO1Array : AO1Array
{
public:
GenericO1Array(int size, Type initVal) : AO1Array(size)
{
        _initVal = initVal;
        Len = size;
        A = new Type[size];
}
~GenericO1Array()
{
        delete[] A;
        A = NULL;
}
int Length() { return Len; }
Type & operator [](int index) const
{
    if (AO1Array::isRealValue(index))
        return A[index];
    return _initVal;
}
Type & operator [] (int index) 
{
    if (AO1Array::isRealValue(index))
        realValue = true;
    else
        realValue = false;
    return A[index];
}
Type operator =(Type value)
{
    if (realValue)
        A[lastIndex] = _initVal;
    else
    {
        AO1Array::C[top] = lastIndex;
        AO1Array::B[lastIndex] = AO1Array::top++;
        A[index] = value;
    }
    return *this;
}

private:
int Len;
int lastIndex;
bool realValue;
Type _initVal;
Type *A;
};

int main()
{
int n = 20;
GenericO1Array<double> darr1(n, 1.1);
GenericO1Array<long> iarr1(n, 2);
int i;
cout << "nLength.darr1 = " << darr1.Length() << endl;
cout << "nLength.iarr1 = " << iarr1.Length() << endl;
for (i = 0; i < n; i += 2)
{
    darr1[i] = i*10.0;
    iarr1[i] = i * 100;
} // for
cout << "ndarr1 = " << endl;
for (i = 0; i < n; i++)
    cout << "darr1[" << i << "] = " << darr1[i] << endl;
cout << "niarr1 = " << endl;
for (i = 0; i < n; i++)
    cout << "iarr1[" << i << "] = " << iarr1[i] << endl;
 } // main

我的程序没有达到'='重载

您正在重载Generic01Array本身的=赋值操作符,但您的代码中没有任何内容实际上直接将值赋给darr1iarr1变量(没有darr1 = ...iarr = ...语句)。这就是为什么没有调用=操作符。

如果你想在用户给数组元素赋值时发生一些事情,你需要创建一个代理类并重载它的=赋值操作符,然后让你的[]操作符返回该代理的一个实例:

template<class Type>
class GenericO1Array : AO1Array
{
public:
    class Proxy;
    friend Proxy;
    class Proxy
    {
    private:
      Generic01Array& _arr;
      int _index;
    public:
        Proxy(Generic01Array &arr, int index) : _arr(arr), _index(index) {}
        operator Type() const
        {
            if (_arr.isRealValue(index))
                _arr.realValue = true;
            else
                _arr.realValue = false;
            return _arr.A[_index];
        }
        Proxy& operator=(const Type &value)
        {
            if (_arr.realValue)
                _arr.A[_arr.lastindex] = _arr._initVal;
            else
            {
                _arr.C[_arr.top] = _arr.lastIndex;
                _arr.B[_arr.lastIndex] = _arr.top++;
                _arr.A[_index] = value;
            }
            return *this;
        }
    };
    ...
    Proxy operator [] (int index) 
    {
        return Proxy(*this, index);
    }
    ...
};

或第二个'[]'完全重载…

有两个[]操作符的重载,一个是const,另一个不是。[]const版本通过返回对数组内部数据的非const引用打破了操作符的const性。它应该返回一个非引用的const值:

const Type operator [](int index) const

[]操作符的非const版本可以返回引用:

Type& operator [](int index)

您没有在Generic01Array类的任何const实例上调用[]运算符,因此只有[]运算符的非const版本应该被调用。