复制构造函数和组合

发布于 2024-12-25 20:29:09 字数 741 浏览 2 评论 0原文

我在类 A 和 B 之间有组合关系,

class A
{
    A(); //default constructor  //EDIT
    A(const A &mA); // copy constructor //EDIT
    virtual ~A();
};


class B
{
B(A *pA); //constructor
B(const B &mB) //copy constructor
virtual ~B(); //EDIT: destructor to eliminate mA and to build the composition
A* mA;
};

我可以用这种方式编写复制构造函数吗:

B(const B &mB, A *pA)

我需要它来保持复制对象之间的组合。 是不是错了?是否存在更好的解决方案? 谢谢

编辑:我会尽力更好地解释我。我想要对象 mB 和对象 mA 的副本。但是,如果在复制构造函数中我写了 mA =mB.mA,我会将地址复制到原始对象 mA。所以我认为我需要一个深层副本而不是一个吞咽副本。我感到困惑,因为现在,从主体开始,我首先复制对象 mA,然后复制 mB。这样做,我认为我需要使用外部函数来分配复制的对象 mA,

foo(A *pA)

否则如果我可以对 mB 进行深度复制,我就可以解决问题。这就是所谓的深拷贝吗?

聚苯乙烯 A和B是抽象类

I have a relationship of composition between class A and B,

class A
{
    A(); //default constructor  //EDIT
    A(const A &mA); // copy constructor //EDIT
    virtual ~A();
};


class B
{
B(A *pA); //constructor
B(const B &mB) //copy constructor
virtual ~B(); //EDIT: destructor to eliminate mA and to build the composition
A* mA;
};

Could I write the copy constructor in this manner:

B(const B &mB, A *pA)

I need it to keep up the composition also between the copied objects.
Is it wrong? Does it exist a better solution?
Thank you

EDIT: I'll try to explain me better. I want a copy of the object mB and the object mA. But if in the copy constructor I had writen mA =mB.mA I would copy the adress to the original object mA. So I think I need a deep copy not an swallow copy. My confusion arise because now, from the main, first I copy the object mA and then I copy mB. Doing that, I think I need to assign the copied object mA with an external function like

foo(A *pA)

Otherwise I could solve the problem if I could doing a deep copy of mB. Is this called a deep copy?

P.S.
A and B are abstract classes

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

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

发布评论

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

评论(2

缱绻入梦 2025-01-01 20:29:09

不。根据定义,复制构造函数不能具有您所描述的签名。以下是复制构造函数的一些有效签名:

B(const B &);
B(B &);   // Thanks Oli!

为什么需要它?您可以通过执行以下操作来访问复制构造函数内的 mA 成员(我可能犯了一些语法错误):

B::B(const B & original)
{
    mA = original.mA;
}

No. By definition, the copy constructor can not have a signature like the one you described. Here are some valid signatures for copy constructors:

B(const B &);
B(B &);   // Thanks Oli!

Why do you need it? You can access the mA member inside the copy constructor by doing something like this (I might have made some syntax errors):

B::B(const B & original)
{
    mA = original.mA;
}
相思碎 2025-01-01 20:29:09

你可能想多了。复制构造函数只需将所有成员初始化为源对象的相应值,例如,如下所示:

B(B const & rhs) : mA(rhs.mA) { }

但这只是简单的复制,因此如果没有其他内容,那么您最好不要在以下位置编写任何复制构造函数全部。

另一方面,如果您想要一个副本,它可能是这样的:

B(B const & rhs) : mA(::new A(rhs.mA)) { }

但是,其细节取决于类B的实际所有权政策到受指点mA。根据这些细节,如有必要,不要忘记编写适当的析构函数。

您还应该为复制构造函数编写一个匹配的赋值运算符来执行一些重要的操作,例如:

B & operator=(B const & rhs)
{
    if (this != &rhs)
    {
        A * tmp = ::new A(*rhs.mA);    // need try/catch in general!
        ::delete mA; // OK, no exception occurred if we got here
        mA = tmp;
    }
    return *this;
}

You might be overthinking this. The copy constructor simply needs to initialize all members to the corresponding values of the source object, e.g. like so:

B(B const & rhs) : mA(rhs.mA) { }

This is just the trivial copy, though, so if there's nothing else to it, then you'd better not write any copy constructor at all.

On the other hand, if you want a deep copy, it could be something like this:

B(B const & rhs) : mA(::new A(rhs.mA)) { }

However, the details of that depend on the actual ownership policy of class B with regard to the pointee mA. Depending on those details, don't forget to write an appropriate destructor, if necessary.

You should also write a matching assignment operator of your copy constructor does something non-trivial, for example:

B & operator=(B const & rhs)
{
    if (this != &rhs)
    {
        A * tmp = ::new A(*rhs.mA);    // need try/catch in general!
        ::delete mA; // OK, no exception occurred if we got here
        mA = tmp;
    }
    return *this;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文