[英]C++ 3D Matrix Using References
Request for a sanity check ... 要求进行健全检查......
I am storing a 3D matrix in a 1D array as follows: 我将3D矩阵存储在一维数组中,如下所示:
double * myVector = new double[NCHANNELS*NROWS*NCOLUMNS];
I want to be able to access myVector using [channel][row][column] syntax, so I am creating a C++ reference: 我希望能够使用[channel] [row] [column]语法访问myVector,所以我创建了一个C ++引用:
double (& my3dMatrix)[NCHANNELS][NROWS][NCOLUMNS] = myVector;
Have I used the proper syntax here, ie, will 我在这里使用了正确的语法,即意志
my3dMatrix[channel][row][column]
return 返回
*(myVector + channel*NROWS*NCOLUMNS + row*NCOLUMNS + column)?
Thanks. 谢谢。
Yes, you can do it. 是的,你可以做到。 But your reference initializator is missing type cast and dereference operator, it will not even compile. 但是您的引用初始化器缺少类型转换和取消引用运算符,它甚至不会编译。 It should be: 它应该是:
double (& my3dMatrix)[NCHANNELS][NROWS][NCOLUMNS] =
*(double (*)[NCHANNELS][NROWS][NCOLUMNS])myVector;
If you're using C++11/14, you can use this fancy syntax instead: 如果您使用的是C ++ 11/14,则可以使用这种精美的语法:
double (& my3dMatrix)[NCHANNELS][NROWS][NCOLUMNS] =
*(decltype(&my3dMatrix))myVector;
double (& my3dMatrix)[NCHANNELS][NROWS][NCOLUMNS] =
*reinterpret_cast<decltype(&my3dMatrix)>(myVector);
or C++11 version: 或C ++ 11版本:
double (& my3dMatrix)[NCHANNELS][NROWS][NCOLUMNS] = *reinterpret_cast<decltype(&my3dMatrix)>(myVector);
Syntax explanation 语法解释
When you initialize a reference, you can only use initializator of a certain type. 初始化引用时,只能使用某种类型的初始化器。 This type must match the type reference refers to. 此类型必须与引用的类型匹配。
double (& my3dMatrix)[NCHANNELS][NROWS][NCOLUMNS] = expression;
In this case you need expression
to be double [NCHANNELS][NROWS][NCOLUMNS]
. 在这种情况下,你需要expression
为double [NCHANNELS][NROWS][NCOLUMNS]
。
myVector
can't be used here because it's a pointer to array and my3dMatrix
is a reference to an array . myVector
可以因为它是一个指针数组,并不能在此处使用my3dMatrix
是数组的引用。 Ok, then we need to deference operator. 好的,那么我们需要尊重运算符。
But *myVector
does not work either. 但是*myVector
也不起作用。 It's double [NCHANNELS*NROWS*NCOLUMNS]
but we need double [NCHANNELS][NROWS][NCOLUMNS]
. 它是double [NCHANNELS*NROWS*NCOLUMNS]
但我们需要double [NCHANNELS][NROWS][NCOLUMNS]
。 You want to tell the compiler than he should assume that myVector
is pointing to another type before dereferencing it. 你想告诉编译器,他应该假设myVector
在解除引用之前指向另一个类型。 In these cases you need a type cast. 在这些情况下,您需要进行类型转换。
The way you write them is (type)expression
and reinterpret_cast<type>(expression)
. 你写它们的方式是(type)expression
和reinterpret_cast<type>(expression)
。
(First one came from C language. Second one is one of the modern C++ casts. There are 4 of them, each one have a specific purpose. C-style cast does the same and can be used instead of all 4. C++-style ones are used by some guys because they think that these casts are more readable. I personally consider them too bulky and use old one.) (第一个来自C语言。第二个是现代C ++演员之一。其中有4个,每个都有一个特定的目的.C风格的演员也是如此,可以用来代替所有4. C ++风格一些人使用的是因为他们认为这些演员阵容更具可读性。我个人认为它们太笨重而且使用旧版本。)
So, here is your reference initialization: 那么,这是你的参考初始化:
double (& my3dMatrix)[NCHANNELS][NROWS][NCOLUMNS] = * (double (*)[NCHANNELS][NROWS][NCOLUMNS]) myVector; ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^ dereference C-style type cast expression
Compiler trusts you and assumes that myVector is a double (*)[NCHANNELS][NROWS][NCOLUMNS]
. 编译器信任您并假设double (*)[NCHANNELS][NROWS][NCOLUMNS]
是double (*)[NCHANNELS][NROWS][NCOLUMNS]
。 After dereferncing it becomes double [NCHANNELS][NROWS][NCOLUMNS]
- the type you need. 在取消之后,它变成了double [NCHANNELS][NROWS][NCOLUMNS]
- 你需要的类型。
Now, about that decltype
trick. 现在,关于那个decltype
技巧。 (It was added in 2011, so old compilers don't support it.) decltype(expression)
is replaced by the compiler with the type of expression
. (它是在2011年添加的,所以旧的编译器不支持它。) decltype(expression)
被编译器替换为expression
。 For example, decltype(1)
means int
. 例如, decltype(1)
表示int
。 decltype(my3dMatrix)
means double [NCHANNELS][NROWS][NCOLUMNS]
. decltype(my3dMatrix)
表示double [NCHANNELS][NROWS][NCOLUMNS]
。 But you need another type, because you can't cast pointer to 1D array to 3D array . 但是你需要另一种类型,因为你不能将指向1D数组的指针投射到3D数组 。 You can cast it to pointer to 3D array instead. 您可以将其转换为指向3D数组的指针 。 When you want address of something, you use &
. 当你想要某事的地址时,你使用&
。 So, you need decltype(&my3dMatrix)
. 所以,你需要decltype(&my3dMatrix)
。 You can assume it means (double (&*)[NCHANNELS][NROWS][NCOLUMNS])
, but there is no such thing as pointer to reference, so it becomes just (double (*)[NCHANNELS][NROWS][NCOLUMNS])
. 你可以认为它意味着(double (&*)[NCHANNELS][NROWS][NCOLUMNS])
,但没有指向引用的东西,所以它变成了(double (*)[NCHANNELS][NROWS][NCOLUMNS])
。
double (& my3dMatrix)[NCHANNELS][NROWS][NCOLUMNS] = * (decltype(&my3dMatrix)) myVector; ^ ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^ dereference C-style type cast expression
I hope you understood it. 我希望你理解它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.