Ben*_*nes 3 c++ g++ pass-by-reference
当我调用一个引用的方法时,g ++抱怨我没有传递引用.我认为呼叫者不必为PBR做任何不同的事情.这是有问题的代码:
//method definition
void addVertexInfo(VertexInfo &vi){vertexInstances.push_back(vi);}
//method call:
sharedVertices[index]->addVertexInfo(VertexInfo(n1index, n2index));
Run Code Online (Sandbox Code Playgroud)
这是错误:
GLUtils/GLMesh.cpp: In member function 'void GLMesh::addPoly(GLIndexedPoly&)':
GLUtils/GLMesh.cpp:110: error: no matching function for call to 'SharedVertexInfo::addVertexInfo(VertexInfo)'
GLUtils/GLMesh.h:93: note: candidates are: void SharedVertexInfo::addVertexInfo(VertexInfo&)
Jam*_*lis 12
VertexInfo(n1index, n2index)创建一个临时VertexInfo对象.临时不能绑定到非const引用.
修改addVertexInfo()函数以获取const引用将解决此问题:
void addVertexInfo(const VertexInfo& vi) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
通常,如果函数不修改它通过引用获取的参数,它应该采用const引用.