有人可以解释第 12 行发生了什么吗?如何将函数设置为等于结构(矩阵);
mat4& getCurrentMatrix() {
if(currentMatrixMode == MGL_PROJECTION) {
return projMatrix;
}
else { //Not sure if we need to account for MGL_TEXTURE or MGL_COLOR
//yet
return modelViewMatrix;
}
}
void setCurrentMatrix(mat4 matrix) {
getCurrentMatrix() = matrix; //what is going on in here?
最佳答案
您不是将函数设置为等于某个其他对象,而是将函数的结果分配给另一个对象。
如果函数返回某个对象或值(但没有引用),则它没有任何用处(除非 =
运算符被副作用覆盖!),例如在 C 语言中。
但在 C++ 中,您可以返回全局/更高范围对象的引用
它做了一些有用的事情,因为该函数返回对某个全局/更高范围对象的引用,因此它等同于:
modelViewMatrix = matrix;
或
projMatrix = matrix;
取决于函数的参数
关于c++ - 调用函数并将其设置为等于结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49022167/