【发布时间】:2021-07-20 19:42:10
【问题描述】:
我正在尝试了解矩阵乘法的访问运算符。
template<typename T>
class Matrix44
{
public:
Matrix44() {}
// The next two lines are totally confusing for me
const T* operator [] (uint8_t i) const { return m[i]; }
T* operator [] (uint8_t i) { return m[i]; }
// initialize the coefficients of the matrix with the coefficients of the identity matrix
T m[4][4] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}}; // Why can you do this m[4][4]
};
typedef Matrix44<float> Matrix44f;
所以我的理解是他们定义了一个自己的访问运算符来访问矩阵索引:
Matrx44f mat;
mat[0][3] = 1.f;
但这与他们的定义有什么关系
...
const T* operator [] (uint8_t i) const { return m[i]; }
T* operator [] (uint8_t i) { return m[i]; }
非常感谢您帮助 C++ 菜鸟
来源:https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/geometry/matrices
【问题讨论】:
-
老实说,这是一个相当糟糕的矩阵实现,它通过其接口泄露了实现细节。 适当的实现不会这样做。
-
感谢您的重播,但很高兴知道然后我不会深入挖掘:D
-
您是否缺少指针可以像数组一样被索引的点? (如果
p是一个指针,p[k]等价于*(p + k),mat[0]是一个指针。)为了更神秘,考虑你也可以写3[mat[0]] = 1.f;... -
@KonradRudolph 您是否查看过任何主要实现的 STL 头文件?不确定您对 proper 的定义如何适用。
-
@molbdnilo 哈哈,你的最后一句话很棒。更多的谜团正在增加;)
标签: c++