调用功能并将其设置为等于结构

calling a function and setting it equal to a struct

本文关键字:于结构 结构 功能 设置 调用      更新时间:2023-10-16

有人可以解释第12行中的情况吗?我如何设置一个等于struct(矩阵(的函数;

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;

取决于函数的参数

getCurrentMatrix功能返回对mat4的引用。引用的是将其设置为等于传递给函数的矩阵的mat4