指向对象的指针向量,需要向量的深拷贝,但对象是继承对象的基础

发布于 2024-10-03 19:28:53 字数 755 浏览 5 评论 0原文

我想要一个带有指向对象的指针的向量的深层副本,但该对象可以是 C 或 B。我知道令人困惑(我解释它的方式),让我举例说明。

class A {
    A(const A& copyme) { }
    void UnableToInstantiateMeBecauseOf() =0;
};

class B {
    B(const B& copyme) : A(copyme) {}
};

class C {
    C(const C& copyme) : A(copyme) {}
};

std::vector<A*>* CreateDeepCopy(std::vector<A*>& list)
{
    std::vector<A*>* outList = new std::vector<A*>();

    for (std::vector<A*>::iterator it = list.begin(); it != list.end(); ++it)
    {
        A* current = *it;
        // I want an copy of A, but it really is either an B or an C
        A* copy = magic with current;
        outList->push_back(copy);
    }

    return outList;
}

如何创建一个您不知道其继承类型的对象的副本?

I want to have a deep copy of an vector with pointers to objects, but the object can either be C or B. I know confusing (the way I explain it), let me illustrate.

class A {
    A(const A& copyme) { }
    void UnableToInstantiateMeBecauseOf() =0;
};

class B {
    B(const B& copyme) : A(copyme) {}
};

class C {
    C(const C& copyme) : A(copyme) {}
};

std::vector<A*>* CreateDeepCopy(std::vector<A*>& list)
{
    std::vector<A*>* outList = new std::vector<A*>();

    for (std::vector<A*>::iterator it = list.begin(); it != list.end(); ++it)
    {
        A* current = *it;
        // I want an copy of A, but it really is either an B or an C
        A* copy = magic with current;
        outList->push_back(copy);
    }

    return outList;
}

How to create an copy of an object of which you don't what inherited type it is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假面具 2024-10-10 19:28:53

使用克隆:

复制对象 - 保持多态性

class Super
{
public:
    Super();// regular ctor
    Super(const Super& _rhs); // copy constructor
    virtual Super* clone() const = 0; // derived classes to implement.
}; // eo class Super


class Special : public Super
{
public:
    Special() : Super() {};
    Special(const Special& _rhs) : Super(_rhs){};
    virtual Special* clone() const {return(new Special(*this));};
}; // eo class Special

编辑:

我在你的问题中注意到你的基类是抽象的。没关系,这个模型仍然有效,我已经修改了。

Use cloning:

Copy object - keep polymorphism

class Super
{
public:
    Super();// regular ctor
    Super(const Super& _rhs); // copy constructor
    virtual Super* clone() const = 0; // derived classes to implement.
}; // eo class Super


class Special : public Super
{
public:
    Special() : Super() {};
    Special(const Special& _rhs) : Super(_rhs){};
    virtual Special* clone() const {return(new Special(*this));};
}; // eo class Special

EDIT:

I noticed in your question your base-class is abstract. That's fine, this model still works, I have amended.

旧城空念 2024-10-10 19:28:53

将虚拟 Clone() 方法添加到您的类中。

A* copy = it->Clone();

class A {
    virtual A* Clone()
    {
        return new A(*this);
    }
};

重写派生类中的克隆。实现与 A 类相同。

Add a virtual Clone() method to your classes.

A* copy = it->Clone();

class A {
    virtual A* Clone()
    {
        return new A(*this);
    }
};

Override Clone in derived classes. The implementation is the same as with class A.

っ〆星空下的拥抱 2024-10-10 19:28:53

您可以在类 A 中实现纯虚拟克隆函数。

You could implement a pure virtual clone function in class A.

路弥 2024-10-10 19:28:53

正如其他人所说,您需要某种类型的克隆机制。您可能想查看 Kevlin Henney 在他的优秀论文中的 cloning_ptr 单独克隆

As others have said you need some type of cloning mechanism. You might want to check out the cloning_ptr by Kevlin Henney in his excellent paper Clone Alone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文