Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setter function for a vector<someClass> in c++

I have the following classes:

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? 

    }

};

Do I need to create a copy constructor here? Overload the operator = for Vertex and Obj?

like image 624
andandandand Avatar asked Oct 13 '22 19:10

andandandand


1 Answers

Since your Vertex has only primitive non-pointer members, you don't necessarily need to define a copy constructor for it: the compiler will generate one for you that copies the elements by their copy constructors (in the case of a float, that's usually a bitwise copy). The copy constructor and assignment operator for std::vector are predefined and will work here because you are not storing pointers.

(For an std::vector<Vertex *>, the ownership semantics would not be clear so you might need to copy in a different way.)

like image 148
Fred Foo Avatar answered Nov 09 '22 11:11

Fred Foo