Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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
Posted
Updated 10-Oct-10 23:51pm
v2

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();
}


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_;
};


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_;
};


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
 
Share this answer
 
Do you mean this:
C++
#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();
}


?
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900