在堆上动态创建类实例的副本 [英] Creating copy of class instance dynamically on heap

查看:56
本文介绍了在堆上动态创建类实例的副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在写我的课程的复制构造函数(比如说A类).在此类中,我有一个成员,该成员指向在堆上动态创建的其他类(B)的指针(使用new运算符).

我需要使用B类副本构造函数为A副本创建B类的新实例.我该怎么办?

示例代码:

Hi
I''m writing copy constructor of my class (say class A). In this class I have a member which is a pointer to other class (B) created dynamically on the heap (using new operator).

I need to create new instance of class B for the copy of A, using class B copy constructor. How can I do this?

Example code:

//////////////////////////////
class B
{
    // ...
    // unimportant stuff
    // ...
};

/////////////////////////////
class A
{
    B *pB;

    //-----------------------------
    A() : pB(NULL) 
    {
    }

    //-----------------------------
    A(const A& _a)
    {
        if (pB != NULL)
        {
            // ?? how to create a copy of *pB?
            // pB = new B(*pB); doesn''t work
        }
    }

    //-----------------------------
    void Method1()
    {
        pB = new B();
    }
};



感谢您的帮助



Thanks for any help

推荐答案

您的意思是:
Do you mean this:
#include <iostream>
//////////////////////////////
class B
{
    // ...
    // unimportant stuff
    // ...
  public:
  B(){/*...*/}
  B(const B & b){/*...*/}
  B & operator=(const B & b){/*...*/}
};
/////////////////////////////
class A
{
    B *pB;

public:
    //-----------------------------
    A() : pB(NULL)
    {
    }
    //-----------------------------
    A(const A& _a)
    {
        if (_a.pB != NULL)
        {
          pB = new B(*_a.pB);
        }
    }
    //-----------------------------
    void Method1()
    {
        pB = new B();
    }
    void dump()
    {
      std::cout << pB << std::endl;
    }
};
int main()
{
  A a1;
  a1.Method1();
  A a2(a1);
  a1.dump();
  a2.dump();
}



?
:)



?
:)


CPallini的答案涵盖了您的直接问题,当您开始使用动态对象和原始指针时,请当心打开一大堆蠕虫.例如Method1()是等待发生的资源泄漏:

While CPallini''s answer covers your immediate problem watch out for opening a big can of worms when you start using dynamic objects and raw pointers. For example Method1() is a resource leak waiting to happen:

void Method1()
{
    // What happens if pB is already non-zero??
    pB = new B();
}



而且你也没有破坏者.

因此,请考虑使用以下两种方法之一:

-删除指针,只包含B的一个实例.该类也简化了很多:



and you haven''t got a destructor either.

So consider using one of two things:

- get rid of the pointer and just contain an instance of B. The class simplifies a lot as well:

class A
{
     private:
        B b_;
};



嗯,就是这样.您不需要实现副本构造,副本分配或析构函数.

-将指针替换为类似对象的指针,例如std :: auto_ptr.这意味着您不需要析构函数,并且可以简化默认的构造函数:



Er, that''s it. You don''t need to implement copy construction, copy assignment or a destructor.

- replace the pointer with a pointer like object, e.g. std::auto_ptr. This means you won''t need a destructor and it simplifies your default constructor:

class A
{
    public:
        A( const A &a )
        {
            if( a.b_.get() ) b_.reset( new B( *b_ ) );
        }

    private:
        std::auto_ptr<B> b_;
};



就个人而言,我会选择直成员,因为它要简单得多,除非有充分的理由动态创建所包含的对象(例如B很大且并非总是创建),否则您不会花很多钱.

干杯,



Personally I''d go for the straight member as it''s a lot simpler and, unless there''s a good reason for dynamically creating the contained object (like B is massive and not always created), doesn''t buy you much.

Cheers,

Ash


这篇关于在堆上动态创建类实例的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆