深度复制派生类中的指针向量
我有一个名为 Cipher 的纯虚拟类
class Cipher
{
public:
//This class doesn't have any data elements
virtual Cipher* clone() const = 0;
virtual ~Cipher() { };
//The class has other functions as well, but they are not relevant to the question
};
Cipher 还有一些其他派生类(例如 CaesarCipher)。问题是关于 CipherQueue,它看起来像这样:
//I've tried to only include the relevant parts here as well
class CipherQueue: public Cipher
{
std::vector<Cipher*> tarolo;
public:
void add(Cipher* cipher)
{tarolo.push_back(cipher);}
CipherQueue(const CipherQueue& _rhs); //?????
CipherQueue* clone() const; //?????
~CipherQueue()
{
for (size_t i = 0; i < tarolo.size(); i++)
{
delete tarolo[i];
}
//tarolo.clear(); //not sure if this is needed
}
CipherQueue 有一个名为 tarolo 的向量。它包含指向 Cipher 派生类的指针。您可以使用 new 运算符或给定类的克隆函数(已实现)向此向量添加元素:
CipherQueue example;
example.add(new CaesarCipher(3))
CaesarCipher c(6);
example.add(c.clone());
//It is the job of the CipherQueue class to free up the memory afterwards in both cases
现在的问题是:如何实现复制构造函数和CipherQueue中的clone函数,以便在clone函数中使用复制构造函数,clone函数创建对象的深拷贝它被称为? 我已经制作了我认为的浅复制,这不好,因为析构函数 ~CipherQueue()
不适用于浅复制。 (或者析构函数也可能是错误的?) 目标是让你可以做这样的事情:
CipherQueue example;
CipherQueue inside; //Let's say that this already has a few elements in it
example.add(inside.clone());
example.add(example.clone()); //This should also work
这是我之前尝试过的,不使用复制构造函数(我认为这是一个浅复制,因此它会导致我的程序出现分段错误):
CipherQueue* clone() const
{
CipherQueue* to_clone = new CipherQueue;
to_clone->tarolo = this->tarolo;
return to_clone;
}
I have a pure virtual class called Cipher
class Cipher
{
public:
//This class doesn't have any data elements
virtual Cipher* clone() const = 0;
virtual ~Cipher() { };
//The class has other functions as well, but they are not relevant to the question
};
Cipher has a few other derived classes (for example CaesarCipher). The question will be about CipherQueue, which looks something like this:
//I've tried to only include the relevant parts here as well
class CipherQueue: public Cipher
{
std::vector<Cipher*> tarolo;
public:
void add(Cipher* cipher)
{tarolo.push_back(cipher);}
CipherQueue(const CipherQueue& _rhs); //?????
CipherQueue* clone() const; //?????
~CipherQueue()
{
for (size_t i = 0; i < tarolo.size(); i++)
{
delete tarolo[i];
}
//tarolo.clear(); //not sure if this is needed
}
CipherQueue has a vector called tarolo. It contains pointers to the derived classes of Cipher. You can add elements to this vector by using the new operator, or the clone function (which has already been implemented) of the given class:
CipherQueue example;
example.add(new CaesarCipher(3))
CaesarCipher c(6);
example.add(c.clone());
//It is the job of the CipherQueue class to free up the memory afterwards in both cases
Now the question is: how can I implement the copy constructor and the clone function in CipherQueue, so that the copy constructor is used in the clone function, and the clone function creates a deep copy of the object that it is called on?
I've already made what I think is a shallow copy, which isn't good, because the destructor, ~CipherQueue()
doesn't work with a shallow copy. (Or could the destructor be wrong as well?)
The goal is to make it so that you can do something like this:
CipherQueue example;
CipherQueue inside; //Let's say that this already has a few elements in it
example.add(inside.clone());
example.add(example.clone()); //This should also work
Here's what I've tried before, without using the copy constructor (which is a shallow copy I think, and therefore it causes my program to get a segmentation fault):
CipherQueue* clone() const
{
CipherQueue* to_clone = new CipherQueue;
to_clone->tarolo = this->tarolo;
return to_clone;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谢谢Marius Bancila,您是对的,问题在于我只是在复制指针,而不是在新矢量中创建新对象。我在每个对象上打电话给clone(),现在它运行得很好!这是工作克隆功能,以防将来有人偶然发现这个线程:
最后,我似乎根本不需要复制构造函数。您可以在没有它的情况下使克隆功能。
Thank you Marius Bancila, you are right, the problem was that I was just copying the pointers, and not creating new objects in the new vector. I called clone() on each object, and now it works perfectly! Here is the working clone function, in case anyone stumbles upon this thread in the future:
In the end, it seems like I didn't need the copy constructor at all. You can make the clone function without it.