4

I have this code for copying a polygon class. The problem I have is that in the end the vertices point to the original polygon class location. Since the copy constructor does not seem to be invoked. Why is that ?

Polygon::Polygon(const Polygon &aPolyToCopy)
{
int i;
vertices = new Vertex[aPolyToCopy.count];

for (i=0;i<aPolyToCopy.count;i++)
{
    vertices[i].x = aPolyToCopy.vertices[i].x;
    vertices[i].y = aPolyToCopy.vertices[i].y;
}

count = aPolyToCopy.count;
}

In a list template I do this

template <class T, int i>
bool SortedVector<T, i>::add ( const T& v )
{
myClass[myCurrent] = v; //Copy constructor not called ?
myCurrent++;

return true;
}

The template is

 template <class T, int i>
 class SortedVector
 {
 public:
   int maxSize;
   T myClass[i];
   int myCurrent;

   SortedVector();
   ~SortedVector();
   bool add ( const T& v );
};
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
user2130951
  • 2,601
  • 4
  • 31
  • 58

2 Answers2

3

You're doing an assignment, you don't construct a new object here. If you define a custom copy constructor, you also need to overload operator=

See http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/ for example.

If you would do something like x = Polygon(y), then your copy constructor would be called (followed by the default operator=). But don't use this workaround, just provide your operator=.

Philipp
  • 11,549
  • 8
  • 66
  • 126
1

I think a problem in your Polygon class is that you have a vertices data member, which seems to be a raw pointer to a Vertex, used to store a raw array allocated with new[]:

vertices = new Vertex[aPolyToCopy.count];

You may need to overload also operator= (and the destructor), not only the copy constructor (see The Rule of Three); you haven't showed all the code of your Polygon class, so it's not clear if you defined proper copy assignment and destruction.

Note that you will simplify your code if you use a robust RAII container class like std::vector. Just add a "std::vector<Vertex> vertices;" data member instead of a "Vertex* vertices" data member, and std::vector will take care of copy, cleanup, etc. You don't need to do anything: it's all automatically managed by std::vector.

#include <vector>    // for std::vector

class Polygon
{
  std::vector<Vertex> vertices;

public:
  explicit Polygon(size_t vertexCount)
    : vertices(vertexCount) // Build a polygon with specified vertices
  {}

  //  
  // Compiler generated copy constructor, operator= and destructor are fine.
  //
};

In general, in C++ try to build classes assembling together convenient RAII building blocks like std::vector and other direct resource managers.

Community
  • 1
  • 1
Mr.C64
  • 41,637
  • 14
  • 86
  • 162