简体   繁体   English

为什么不调用复制构造函数?

[英]Why doesn't the copy constructor get called?

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 );
};

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= 如果定义自定义复制构造函数,则还需要重载operator =

See http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/ for example. 例如,请参阅http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/

If you would do something like x = Polygon(y) , then your copy constructor would be called (followed by the default operator= ). 如果您要执行x = Polygon(y) ,则会调用复制构造函数(后跟默认的operator= )。 But don't use this workaround, just provide your operator= . 但是请不要使用此解决方法,只需提供您的operator=

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[] : 我认为你的Polygon类中的一个问题是你有一个vertices数据成员,它似乎是一个指向 Vertex原始指针 ,用于存储用new[]分配的原始数组

vertices = new Vertex[aPolyToCopy.count]; 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 ); 您可能还需要重载operator= (和析构函数),而不仅仅是复制构造函数(参见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. 您没有显示Polygon类的所有代码,因此不清楚您是否定义了正确的副本分配和销毁。

Note that you will simplify your code if you use a robust RAII container class like std::vector . 请注意,如果使用强大的RAII容器类(如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 . 只需添加一个“ std::vector<Vertex> vertices; ”数据成员而不是“ Vertex* vertices ”数据成员, std::vector将负责复制,清理等。您不需要做任何事情:它全部由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. 通常,在C ++中尝试构建将诸如std::vector和其他直接资源管理器等方便的RAII构建块组合在一起的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM