5

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?

Chris Barlow
  • 3,274
  • 4
  • 31
  • 52
andandandand
  • 21,946
  • 60
  • 170
  • 271

4 Answers4

3

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.)

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • `Vertex` is no POD because it has a user-declared constructor - see e.g. [here](http://stackoverflow.com/questions/146452/what-are-pod-types-in-c/146589#146589). – Georg Fritzsche Dec 18 '10 at 16:24
  • 1
    It does not use bitwaise copy. It uses each members copy constructor. So its as if you did x.m1 = y.m1 etc. – Martin York Dec 18 '10 at 17:18
  • @Martin York: True, but in the case of a `float` that commonly boils down to a bitwise copy. Updated, though. – Fred Foo Dec 18 '10 at 17:20
1

The copy constructor you get free from the compiler will do just fine here. Same goes for the assignment operator you get free of charge or bytes in your source code. The constructor you supplied however eliminates the default constructor the compiler gives, and you need a default constructor for your object to sit in standard containers.

wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
0

You probably don't need to overload these since you don't have any pointers or unsharable references thus the default copy constructor\assignment operator can handle it correctly.

stnr
  • 435
  • 4
  • 14
0

You should probably use a POD not a class anyhow:

struct Vertex { float x; float y; float z; };

To work around the lack of constructor use factories:

inline Vertex mk_Vertex (float x, float y, float z) {
  Vertex a; a.x = x; a.y=y; a.z=z; return a; }

inline Vertex mk_Planar (float x, float y) {
  Vertex a; a.x=y; a.y=y; a.z=0.0f; }

This gives you multiple named constructors, and leaves Vertex a POD, which means you can also use C style initialisers:

Vertex a = {1.0f, 2.0r, 3.0f };

which can be quite useful for higher order aggregates such as arrays, or for mapping images on and off disk.

Yttrill
  • 4,725
  • 1
  • 20
  • 29