简体   繁体   English

深度复制派生的指针向量 class

[英]Deep copying a vector of pointers in derived class

I have a pure virtual class called Cipher我有一个名为 Cipher 的纯虚拟 class

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). Cipher 有一些其他派生类(例如 CaesarCipher)。 The question will be about CipherQueue, which looks something like this:问题将与 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 has a vector called tarolo . CipherQueue 有一个名为tarolo的向量。 It contains pointers to the derived classes of Cipher.它包含指向 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:您可以使用 new 运算符或给定 class 的克隆 function(已实现)向此向量添加元素:

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?现在的问题是:如何在 CipherQueue 中实现复制构造函数克隆 function 以便在克隆 function 中使用复制构造函数,而克隆 function 创建调用它的 object 的深层副本 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.我已经做了我认为是浅拷贝的东西,这不好,因为析构函数~CipherQueue()不适用于浅拷贝。 (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;
    }

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.谢谢 Marius Bancila,你是对的,问题是我只是复制指针,而不是在新向量中创建新对象。 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:我在每个 object 上调用了 clone(),现在它运行良好,这是工作克隆 function:以防将来有人偶然发现这个线程:

    CipherQueue* clone() const
    {
        CipherQueue* to_clone = new CipherQueue;
        to_clone->tarolo = this->tarolo; //First we copy the pointers
        for (size_t i = 0; i < this->tarolo.size(); i++)
        {
            to_clone->tarolo[i] = tarolo[i]->clone(); //Then we use the clone function on each element to actually create new objects, and not just have copied pointers
        }
        return to_clone;
    }

In the end, it seems like I didn't need the copy constructor at all.最后,似乎我根本不需要复制构造函数。 You can make the clone function without it.您可以在没有它的情况下制作克隆 function。

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

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