我有以下类(class):
class Vertex {
public: float X;
float Y;
float Z;
Vertex (float first, float second, float third){
X=first;
Y=second;
Z=third;
}
};
class Obj {
vector<Vertex>vertexCoordinates;
vector<vector<int>> faces;
vector <vector<float>> faceNormals;
vector <vector<float>> faceCenters;
string objName;
int vertexCount, faceCount, edgeCount;
float maxX, minX, maxY, minY, maxZ, minZ, dx, dy, dz;
setVertexCoordinates(vector <Vertex> vertexCoordinatesP) {
vertexCoordinates = vertexCoordinatesP; //??
// How should the assignment be defined?
}
};
我需要在这里创建一个拷贝构造函数吗?为 Vertex
和 Obj
重载运算符 =
?
最佳答案
由于您的 Vertex
只有基本的非指针成员,你不一定需要为它定义一个复制构造函数:编译器会generate one for you通过它们的复制构造函数复制元素(在 float
的情况下,通常是按位复制)。 std::vector
的复制构造函数和赋值运算符是预定义的并且可以在这里工作,因为您没有存储指针。
(对于 std::vector<Vertex *>
,所有权语义不明确,因此您可能需要以不同的方式进行复制。)
关于c++ - c++ 中 vector<someClass> 的 setter 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4478708/