使用重载下标运算符"[ ]"获取数组并将其设置为数组的值将不起作用
Getting and setting values to an array with Overloading the subscript operator “[ ]” won't work
在苦苦寻找答案之后…
我尝试了(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
本身的=
赋值操作符,但您的代码中没有任何内容实际上直接将值赋给darr1
或iarr1
变量(没有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
版本应该被调用。
相关文章:
- 我如何处理有关输入的字符串大小超过字符数组设置大小的错误?
- 将字符数组设置为彼此相等的方法不起作用
- 将数组设置在一个对象中,等于另一个对象中的数组
- C++ |如何将整个 2d 数组设置为 O
- 将 2D 数组设置为函数,并根据需要在其他地方调用它
- 使用C 11将数组设置为零
- 将数组设置为包含字符串值会导致错误
- 将数字动态数组设置为数组或容器 c++
- 将对象的数组设置为c 中的null
- 在此初始值设定项方法上将整个数组设置为 NULL 是否C++标准?
- 将结构中的字符串数组设置为函数中的us
- C++如何将整个字符数组设置为空格
- 使用 *指针数组C++设置构造函数和析构函数
- 如何将可变大小的静态数组设置为用 0 填充
- 将double-all的二维数组设置为-1的最快方法
- 使用memset将字符串数组设置为0后,该数组不可用
- 在C++中将多维数组设置为向量
- 将浮点数数组设置为相同值的最快方法
- 为什么使用 'new' 创建的数组设置为 NULL?
- c++使用数组设置交集和并集