glUniforms()和[]运算符重载问题
glUniforms() and [] operator overloading woes
我在编译以下代码时遇到了一些问题:
GLint iModelMatrix = glGetUniformLocation(m_shaderProgramID, "mvMatrix");
glUniformMatrix4fv(iModelMatrix, 1, GL_FALSE, &myMatrix[0]);
GLint iLight = glGetUniformLocation(m_shaderProgramID, "vLightPos");
glUniform3fv(iLight, 1, &myVector[0]);
现在myMatrix和myVector是两个对象,每个类都重载了[]运算符,如下所示:
inline float& operator[](int index) { return m_data[index]; }
inline float operator[](int index) const { return m_data[index]; }
我收到以下错误:
error C2102: '&' requires l-value
我试着理解为什么"&"是否需要左侧值?我以前传递过这样的数据。我不是简单地把地址给m_data数组的第一个索引吗?这难道不构成浮动*吗?
为什么它不满足功能规范?
void glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
编辑:
好吧,因为我将这些对象作为const引用传递给进行上述调用的函数,所以它是按值传递float*数组,因此我不能引用它们。
我试着添加第三个操作员:
inline const float& operator[](int index) const { return m_data[index]; }
但这让我的编译器爆炸了。我不能这样做有什么原因吗?
这:
inline float operator[](int index) const { return m_data[index]; }
应转换为:
inline const float &operator[](int index) const { return m_data[index]; }
如果要获取返回值的地址。您不会添加第三个版本;您将当前的更改为这个。你不能取临时的地址,但你可以取const&
的地址。
就我个人而言,我认为应该有一个只返回float*
或const float*
的特殊函数,就像C++11对std::vector::data所做的那样。这样,您只需调用myMatrix.data()
,而不必执行&...[0]
的拼凑。它是自我记录的,显而易见。
只需将operator[]
的结果存储在本地变量中
float tmp = myMatrix[0];
glUniformMatrix4fv(iModelMatrix, 1, GL_FALSE, &tmp);
这避免了无法作为l-value处理的未命名临时值。
相关文章:
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- <T> 通过模板化运算符重载将 std::complex 乘以双倍
- C++20概念:需要运算符重载
- 使用赋值运算符重载从类中返回jobject
- 在运算符重载定义中使用成员函数(const错误)
- 字节到位运算符重载C++
- 为什么在运算符重载时需要参考?
- 类中 c++ 的运算符 + 重载
- 算术复合运算符重载为非成员
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 交换运算符 + 重载会导致无限递归
- 如何理解新的运算符重载?
- 向量保持复数的运算符重载
- 如何创建运算符重载?
- 链接列表运算符重载没有打印出我想要的内容
- C++:需要帮助了解运算符重载错误
- 使用模板化运算符重载 XOR 运算符失败
- 如何确保接受的C++模板类型使运算符重载?
- 运算符重载使用运算符+添加类模板