c ++中向量<someClass>的setter函数

and*_*and 5 c++ operator-overloading copy-constructor

我有以下课程:

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? 

    }

};
Run Code Online (Sandbox Code Playgroud)

我需要在这里创建一个复制构造函数吗?超负荷运营商=VertexObj

Fre*_*Foo 3

由于您Vertex只有原始非指针成员,因此您不一定需要为其定义复制构造函数:编译器将为您生成一个复制构造函数,通过其复制构造函数复制元素(在 a 的情况下float,通常是按位复制)。复制构造函数和赋值运算符std::vector是预定义的,并且将在这里工作,因为您不存储指针。

(对于std::vector<Vertex *>,所有权语义并不明确,因此您可能需要以不同的方式进行复制。)