-2
我重载了一个运算符[],但它没有按我的意愿工作。重载运算符[]
class A{
int *const e;
const int row, column;
public:
int rows() const
{
return row;
}
int columns() const
{
return column;
}
int size() const
{
return row * column;
}
A(int r, int c)
: row(r), column(c), e(new int[r*c])
{
for (int i = 0; i < r*c; i++)
{
e[i] = 0;
}
}
virtual int *const operator[ ](int r)
{
return &e[r*row];
}
}
例如: 如果我写道:
A a(2, 5)
a[0][0] = 1; // OK
a[0][1] = 2; // OK
a[0][2] = 3; // It's assigned a [0][2] == 3 but also [1][0] == 3
我没有发现问题。它应该为[0] [2]分配一个值3 这段代码有什么问题?
哦好吧,我已经改变了,但我仍然有同样的问题 – Cieja 2014-12-06 09:02:31
如果你改正了它,你不能有同样的问题。你可能有类似的问题,但不一样。 – 2014-12-06 09:14:07
是的,但我的意思是说结果是一样的 – Cieja 2014-12-06 10:46:32