So I've been overloading an operator ()
for a 2d array class. For random tests I was doing, return was set to array[0][index]
. Now passing just the index (row) with column set to 0. If the index is less than row (then we're in the next row) it still returns the data as if I had specified the column?
所以我一直在为一个2d数组类重载一个operator()。对于我正在做的随机测试,return设置为array [0] [index]。现在只传递索引(行),列设置为0.如果索引小于行(那么我们在下一行)它仍然返回数据,就像我指定了列一样?
Basic code sample:
基本代码示例:
class myArr
{
private:
float array[3][3];
public:
myArr::myArr(float a1, float a2, ... ) { array[0][0] = a1; array[0][1] = a2; ... }
// ^ the "..." just means, the same action till float a9/array[2][2] = a9;
float& operator() (unsigned index) { return array[0][index]; }
const float operator() (unsigned index) const { return array[0][index]; }
};
int main()
{
myArr test(1, 2, 3, 4, 5, 6, 7, 8, 9);
std::cout << myArr(4) << std::endl; // Displays 5
myArr(4) = 0; // Set's element 4 to hold value 0
return 0;
}
Strangely this works. I can set/get the 5th element with 1 parameter. Now I want to know why/how that exactly works, surely it should error up with "out of bounds" or something. And finally would this be "safe" or do you strongly suggested not doing this?
奇怪的是,这有效。我可以使用1个参数设置/获取第5个元素。现在我想知道为什么/如何确切地工作,当然它应该错误地与“出界”或其他东西。最后这是“安全的”还是你强烈建议不要这样做?
I rarely use 2D arrays, mostly due to the lack of performance gain from them.
我很少使用2D阵列,主要是因为它们缺乏性能。
2 个解决方案
#1
6
An array is stored in memory contiguously. In C++, multidimensional arrays are stored row-by-row right after each other in memory, so I believe it's a perfectly valid memory to reach beyond the currently row into the next row. Out of bounds would occur if you reached beyond the last row of your 2D array.
数组连续存储在存储器中。在C ++中,多维数组在内存中逐行存储,因此我相信它是一个完全有效的内存,可以超出当前行到下一行。如果超出2D数组的最后一行,则会出现超出界限。
Whether or not this is a nice thing to do is a different question. Clearly, writing code this way results in readers scratching their heads trying to figure out what you're doing, so you want to avoid writing real code this way.
这是否是一件好事,是一个不同的问题。很明显,以这种方式编写代码会导致读者摸不着头脑,试图弄清楚你在做什么,所以你想避免以这种方式编写真正的代码。
If you're declaring a 2D array, then you'd want to keep those semantics throughout the life of the array, so I would strongly suggest not doing this.
如果您要声明一个2D数组,那么您希望在数组的整个生命周期中保留这些语义,因此我强烈建议不要这样做。
#2
0
When you allocate a 2d array, its memory is contiguous. So if you allocate a 3*3 array of floats, you will allocate 9*4 consecutive bytes. This results in:
分配2d数组时,其内存是连续的。因此,如果您分配3 * 3浮点数组,则将分配9 * 4个连续字节。这导致:
&array[0][i] = array + 0*3 + i = array + i = array[i]
So "by chance", you get the right result.
所以“偶然”,你得到了正确的结果。
#1
6
An array is stored in memory contiguously. In C++, multidimensional arrays are stored row-by-row right after each other in memory, so I believe it's a perfectly valid memory to reach beyond the currently row into the next row. Out of bounds would occur if you reached beyond the last row of your 2D array.
数组连续存储在存储器中。在C ++中,多维数组在内存中逐行存储,因此我相信它是一个完全有效的内存,可以超出当前行到下一行。如果超出2D数组的最后一行,则会出现超出界限。
Whether or not this is a nice thing to do is a different question. Clearly, writing code this way results in readers scratching their heads trying to figure out what you're doing, so you want to avoid writing real code this way.
这是否是一件好事,是一个不同的问题。很明显,以这种方式编写代码会导致读者摸不着头脑,试图弄清楚你在做什么,所以你想避免以这种方式编写真正的代码。
If you're declaring a 2D array, then you'd want to keep those semantics throughout the life of the array, so I would strongly suggest not doing this.
如果您要声明一个2D数组,那么您希望在数组的整个生命周期中保留这些语义,因此我强烈建议不要这样做。
#2
0
When you allocate a 2d array, its memory is contiguous. So if you allocate a 3*3 array of floats, you will allocate 9*4 consecutive bytes. This results in:
分配2d数组时,其内存是连续的。因此,如果您分配3 * 3浮点数组,则将分配9 * 4个连续字节。这导致:
&array[0][i] = array + 0*3 + i = array + i = array[i]
So "by chance", you get the right result.
所以“偶然”,你得到了正确的结果。