我有一个函数,它需要一个指向浮点数组的指针。基于其他条件,我知道指针实际上指向一个 2x2 OR 3x3 矩阵。 (实际上内存最初是这样分配的,例如 float M[2][2] )重要的是我想在函数体中做出这个决定,而不是作为函数参数。
void calcMatrix( int face, float * matrixReturnAsArray )
{
// Here, I would much rather work in natural matrix notation
if( is2x2 )
{
// ### cast matrixReturnAsArray to somethingAsMatrix[2][2]
somethingAsMatrix[0][1] = 2.002;
// etc..
}
else if(is3x3)
{ //etc...
}
}
我知道我可以使用模板和其他技术来更好地解决这个问题。我的问题实际上是关于如何在### 评论中制作这样的演员表。使用 C++ 工作。
原文由 NoahR 发布,翻译遵循 CC BY-SA 4.0 许可协议