多维数组:操作符重载 [英] Multidimensional array: operator overloading
问题描述
我有一个多维数组的类:
-
能够创建一个,两个,...,N维阵列与此类
-
如果数组有n维,我想利用N
运算符[]
来得到一个对象:
例如:
A一({2,2,2,2}];
一个[0] [1] [1] [0] = 5;
但数组不是指针的向量从而导致其他载体等等...
所以我想运营商[]返回一个类的对象,直到最后一个维度,然后返回一个整数
这是一个强烈的简化code,但它表明我的问题:
我收到的错误:[错误]不能将'A :: B'到'廉政'在初始化
的#include< cstddef> // nullptr_t,ptrdiff_t的,为size_t
#包括LT&;&iostream的GT; // CIN,COUT ...A级{
私人的:
静态INT *一个;
上市:
静态INT尺寸;
A(INT I = 0){
尺寸= I;
一个=新INT [5];
对于(INT J = 0; J< 5; J ++)A [J] = j的;
}; B类{
上市:
b。话务员[](的std :: ptrdiff_t的);
};
C类:公共B {
上市:
INT和放大器;运算符[](的std :: ptrdiff_t的);
}; b。话务员[](的std :: ptrdiff_t的);
};// INT A ::计数= 0;A :: B A ::运算符[](的std :: ptrdiff_t的我){
乙水库;
如果(尺寸和所述; = 1){
RES = C();
}
其他{
RES = B();
}
dimensions--;
返回水库;
}A :: B A :: B ::运算符[](的std :: ptrdiff_t的我){
乙水库;
如果(尺寸和所述; = 1){
RES = B();
}
其他{
RES = C();
}
dimensions--;
返回水库;
}INT和放大器; A ::Ç::运算符[](的std :: ptrdiff_t的我){
返回*(A + I);
}
诠释主(){
A * OBJ =新的A(5);
INT解析度=物镜[1] [1] [1] [1] [1];
性病::法院LT&;< RES<<的std :: ENDL;
}
的运算符[]
从左至右 OBJ [1评定] [1] ... [1]
,所以 OBJ [1]
返回 B
对象。假设现在你只要中期业绩的obj = [1]
,那么你分配到 B
对象(或在 []
)的 INT $ C多次调用的情况下
C
对象$ C>,但没有从 B转换
或 C
到 INT
。你可能需要写一个转换操作符,如
运营商INT()
{
//转换为这里诠释
}
为 A
, B
和 C
,如重载运算符是不能继承的。
我得到了刚刚由 A
和 B
(当然我写这样的运营商摆脱你的编译错误链接错误,因为有联合国定义的函数)。
另外请注意,如果你想要写的东西像 OBJ [1] [1] ... [1] = 10
,你需要重载运算符=
,因为再有从 INT
到 A
或您的代理对象。
希望这是有道理的。
PS:又见@Oncaphillis'注释
I have a class with a multidimensional array:
it is possible to create a one, two, ..., n dimensional array with this class
if the array has n dimensions, i want to use n
operator[]
to get an object:
example:
A a({2,2,2,2}];
a[0][1][1][0] = 5;
but array is not a vector of pointer which lead to other vectors etc...
so i want the operator[] to return a class object until the last dimension, then return a integer
This is a strongly simplified code, but it shows my problem:
The error i receive: "[Error] cannot convert 'A::B' to 'int' in initialization"
#include <cstddef> // nullptr_t, ptrdiff_t, size_t
#include <iostream> // cin, cout...
class A {
private:
static int* a;
public:
static int dimensions;
A(int i=0) {
dimensions = i;
a = new int[5];
for(int j=0; j<5; j++) a[j]=j;
};
class B{
public:
B operator[](std::ptrdiff_t);
};
class C: public B{
public:
int& operator[](std::ptrdiff_t);
};
B operator[](std::ptrdiff_t);
};
//int A::count = 0;
A::B A::operator[] (std::ptrdiff_t i) {
B res;
if (dimensions <= 1){
res = C();
}
else{
res = B();
}
dimensions--;
return res;
}
A::B A::B::operator[] (std::ptrdiff_t i){
B res;
if (dimensions <=1){
res = B();
}
else{
res = C();
}
dimensions--;
return res;
}
int& A::C::operator[](std::ptrdiff_t i){
return *(a+i);
}
int main(){
A* obj = new A(5);
int res = obj[1][1][1][1][1];
std::cout<< res << std::endl;
}
The operator[]
is evaluated from left to right in obj[1][1]...[1]
, so obj[1]
returns a B
object. Suppose now you just have int res = obj[1]
, then you'll assign to a B
object (or C
object in the case of multiple invocations of []
) an int
, but there is no conversion from B
or C
to int
. You probably need to write a conversion operator, like
operator int()
{
// convert to int here
}
for A
, B
and C
, as overloaded operators are not inherited.
I got rid of your compiling error just by writing such operators for A
and B
(of course I have linking errors since there are un-defined functions).
Also, note that if you want to write something like obj[1][1]...[1] = 10
, you need to overload operator=
, as again there is no implicit conversion from int
to A
or your proxy objects.
Hope this makes sense.
PS: see also @Oncaphillis' comment!
这篇关于多维数组:操作符重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!