Hello,

I have a class that has a pointer to another class as a private member
variable. I now want to create a copy constructor for this class and I
don't get it.

class myClass
{
int m_x;
yourClass * m_yObj;

myClass(myClass* mobj);
};

myClass::myClass(myClass* mobj)
{
m_x = mobj->m_x;
m_yObj = ?????
}

How do I create a deep copy of the m_yObj object? I want new object with
the same values

// Anders
--
English is not my first, or second, language
so anything strange, or insulting, is due to
the translation.
Please correct me so I may improve my English!

Re: Copy constructor by MrAsm

MrAsm
Tue May 29 10:53:34 CDT 2007

On Tue, 29 May 2007 17:37:21 +0200, Anders Eriksson
<andis59@gmail.com> wrote:

>class myClass
>{
> int m_x;
> yourClass * m_yObj;
>
> myClass(myClass* mobj);
>};
>
>myClass::myClass(myClass* mobj)
>{
> m_x = mobj->m_x;
> m_yObj = ?????
>}
>
>How do I create a deep copy of the m_yObj object? I want new object with
>the same values

Assuming the 'yourClass' is copyable using copy constructor:

myClass::myClass( myClass * mobj )
: m_x( mobj->m_x ), m_yObj( NULL )
{
if ( mobj->m_yObj != NULL )
{
m_yObj = new yourClass( *mobj->m_yObj );
}
}

MrAsm

Re: Copy constructor by Doug

Doug
Tue May 29 11:04:09 CDT 2007

On Tue, 29 May 2007 17:37:21 +0200, Anders Eriksson <andis59@gmail.com>
wrote:

>Hello,
>
>I have a class that has a pointer to another class as a private member
>variable. I now want to create a copy constructor for this class and I
>don't get it.
>
>class myClass
>{
> int m_x;
> yourClass * m_yObj;
>
> myClass(myClass* mobj);
>};
>
>myClass::myClass(myClass* mobj)
>{
> m_x = mobj->m_x;
> m_yObj = ?????
>}

That is not a copy ctor. A copy ctor uses a (usually const) reference
parameter, not a pointer:

myClass::myClass(const myClass& mobj)

>How do I create a deep copy of the m_yObj object? I want new object with
>the same values

Assuming yourClass implements a copy ctor:

myClass::myClass(const myClass& mobj)
: m_x(mobj.m_x),
m_yObj(new yourClass(*mobj.m_yObj))
{
}

It doesn't matter here, but it's a good habit to use the member
initialization list when possible. Note that since you own a dynamically
allocated object and are storing it in a plain old pointer, you have to
implement a destructor and assignment operator as well. Using std::auto_ptr
would relieve you from writing the dtor, but it's still critical to write
the assignment operator; see this page for how to make it exception-safe:

http://www.gotw.ca/gotw/059.htm

You also might want to check out the smart pointer classes at boost.org.

--
Doug Harrison
Visual C++ MVP

Re: Copy constructor by MrAsm

MrAsm
Tue May 29 14:42:32 CDT 2007

On Tue, 29 May 2007 11:04:09 -0500, "Doug Harrison [MVP]"
<dsh@mvps.org> wrote:


>Assuming yourClass implements a copy ctor:
>
> myClass::myClass(const myClass& mobj)
> : m_x(mobj.m_x),
> m_yObj(new yourClass(*mobj.m_yObj))
> {
> }

Would be better to init m_yObj to NULL and then do the deep-copy only
if mobj.m_yObj != NULL ?

MrAsm

Re: Copy constructor by MrAsm

MrAsm
Tue May 29 14:43:32 CDT 2007

On Tue, 29 May 2007 15:53:34 GMT, MrAsm <mrasm@usa.com> wrote:

>Assuming the 'yourClass' is copyable using copy constructor:
>
>myClass::myClass( myClass * mobj )
[...]

I apologize: this signature is not the copy ctor.
Doug showed the correct signature.

MrAsm

Re: Copy constructor by Doug

Doug
Tue May 29 14:47:54 CDT 2007

On Tue, 29 May 2007 19:42:32 GMT, MrAsm <mrasm@usa.com> wrote:

>On Tue, 29 May 2007 11:04:09 -0500, "Doug Harrison [MVP]"
><dsh@mvps.org> wrote:
>
>
>>Assuming yourClass implements a copy ctor:
>>
>> myClass::myClass(const myClass& mobj)
>> : m_x(mobj.m_x),
>> m_yObj(new yourClass(*mobj.m_yObj))
>> {
>> }
>
>Would be better to init m_yObj to NULL and then do the deep-copy only
>if mobj.m_yObj != NULL ?

It depends. If mobj.m_yObj can be NULL, it would be a requirement. If it
can't be NULL, it's unnecessary.

--
Doug Harrison
Visual C++ MVP

Re: Copy constructor by Ben

Ben
Tue May 29 16:42:30 CDT 2007


"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:an0p53p6nrqesjkk7n9n75743b7j3bntpl@4ax.com...
> On Tue, 29 May 2007 19:42:32 GMT, MrAsm <mrasm@usa.com> wrote:
>
>>On Tue, 29 May 2007 11:04:09 -0500, "Doug Harrison [MVP]"
>><dsh@mvps.org> wrote:
>>
>>
>>>Assuming yourClass implements a copy ctor:
>>>
>>> myClass::myClass(const myClass& mobj)
>>> : m_x(mobj.m_x),
>>> m_yObj(new yourClass(*mobj.m_yObj))
>>> {
>>> }
>>
>>Would be better to init m_yObj to NULL and then do the deep-copy only
>>if mobj.m_yObj != NULL ?
>
> It depends. If mobj.m_yObj can be NULL, it would be a requirement. If it
> can't be NULL, it's unnecessary.

Better use a static member function...

private:
static yourClass* cloneYObj(const yourClass* const otherYObj)
{
if (otherYObj == NULL)
return NULL;
return new yourClass(*otherYObj);
}

public:
myClass::myClass(const myClass& mobj)
: m_x(mobj.m_x),
m_yObj(cloneYObj(mobj.m_yObj))
{
}

That way you can implement complex logic deciding how to copy, without
breaking const-correctness (which is the main reason for using the
initializer-list).

>
> --
> Doug Harrison
> Visual C++ MVP



Re: Copy constructor by Ulrich

Ulrich
Wed May 30 00:58:42 CDT 2007

Anders Eriksson wrote:

> Hello,
>
> I have a class that has a pointer to another class as a private member
> variable. I now want to create a copy constructor for this class and I
> don't get it.
>
> class myClass
> {
> int m_x;
> yourClass * m_yObj;

yourClass m_yObj;

>
> myClass(myClass* mobj);
> };
>
> myClass::myClass(myClass* mobj)
> {
> m_x = mobj->m_x;
> m_yObj = ?????

m_yObj = mobj->m_yObj;

> }
>
> How do I create a deep copy of the m_yObj object? I want new object with
> the same values

See above inline. All other comments in this thread of course still apply,
but I wanted to point out that using 'new' and pointers in C++ is generally
a bad idea. Don't try to write Java programs in C++, there it would be
perfectly normal, in C++ it's just wrong.

Uli


Re: Copy constructor by Tom

Tom
Thu May 31 05:03:03 CDT 2007

Doug Harrison [MVP] wrote:
> On Tue, 29 May 2007 19:42:32 GMT, MrAsm <mrasm@usa.com> wrote:
>
>> On Tue, 29 May 2007 11:04:09 -0500, "Doug Harrison [MVP]"
>> <dsh@mvps.org> wrote:
>>
>>
>>> Assuming yourClass implements a copy ctor:
>>>
>>> myClass::myClass(const myClass& mobj)
>>> : m_x(mobj.m_x),
>>> m_yObj(new yourClass(*mobj.m_yObj))
>>> {
>>> }
>> Would be better to init m_yObj to NULL and then do the deep-copy only
>> if mobj.m_yObj != NULL ?
>
> It depends. If mobj.m_yObj can be NULL, it would be a requirement. If it
> can't be NULL, it's unnecessary.

If it can be 0, you can also just do:

myClass::myClass(const myClass& mobj)
: m_x(mobj.m_x),
m_yObj(mobj.m_yObj ? new yourClass(*mobj.m_yObj) : 0)
{
}

Tom

Re: Copy constructor by Doug

Doug
Thu May 31 10:23:47 CDT 2007

On Thu, 31 May 2007 11:03:03 +0100, "Tom Widmer [VC++ MVP]"
<tom_usenet@hotmail.com> wrote:

>If it can be 0, you can also just do:
>
> myClass::myClass(const myClass& mobj)
> : m_x(mobj.m_x),
> m_yObj(mobj.m_yObj ? new yourClass(*mobj.m_yObj) : 0)
> {
> }

That is indeed what I do.

--
Doug Harrison
Visual C++ MVP