Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Deep versus Shallow Copy - Part 3

Reply
Thread Tools

Deep versus Shallow Copy - Part 3

 
 
blangela
Guest
Posts: n/a
 
      11-26-2006
3.0 Advanced Topic Addendum

There are a few cases where the C++ compiler cannot provide an
overloaded assignment operator for your class. If your class contains
a const member or/and a reference member, the compiler will not be able
to synthesize an assignment operator for your class. It actually helps
to think of a reference member as a const member (since it cannot be
made to reference any other object once it has been initialized).

Interestingly, the compiler will be able to synthesize a copy
constructor for a class that defines a const member or a reference
member, but the compiler supplied copy constructor will do a shallow
copy of any reference members that are defined for the class.

In the case where your class contains a reference member, you will
likely want to overload the assignment operator for your class (since
the compiler cannot do it for you).

Here is some sample code:

class ABC // dummy class used below
{};

class Example3
{
public:
Example3(ABC &); // ctor (this class has no default ctor)
Example3( const Example3 &); // copy ctor
Example3 & operator = (const Example3 &); // overloaded
// assignment operator
private:
int ii;
double dd;
ABC & abcRef; // will be made to reference an ABC object
// in the ctor MIL
const long Cll; // must be init. in ctor MIL
};


// ctor - Note: both abcRef must be init. in the MIL,
// just as a const member would need to be
Example3::Example3(ABC & abcObj)
: abcRef(abcObj), Cll(1234567890)
{
ii = 10; // assign some value
dd = 3.765; // assign some value
}


// copy ctor - compiler supplied copy ctor would do the same
Example3::Example3(const Example3 & src)
:ii(src.ii), dd(src.dd), abcRef(src.abcRef), Cll(src.Cll)// MIL
{}


// overloaded assignement operator
Example3 & Example3:perator = (const Example3 & RHS)
{
if (this == &RHS) // if true, we are done!
{
return *this; // return the original LHS operand
}
else // we still have some work to do
{
ii = RHS.ii; // a shallow copy is good enough here
dd = RHS.dd; // here as well

abcRef = RHS.abcRef; // this does a deep copy!!!
// Cll = RHS.Cll; We cannot do this, since Cll is const

return *this; // return the modified LHS operand.
}
}

// Simple program to test our Example3 class
int main()
{
ABC abc_1,abc_2; // Create 2 ABC objects

Example3 ex2_1(abc_1), ex2_2(abc_2);// Will invoke ctor (twice).

ex2_1 = ex2_2; // Will invoke overloaded Assignment operator.

Example3 ex2_3(ex2_1); // Will invoke copy ctor.

return 0; // We are finished!

} // end of main()

The overloaded assignment operator for the Example3 class above does a
deep copy of the reference member. It cannot do anything with the Cll
member since Cll is const.

The copy constructor for the Example3 class must of course do a shallow
copy of the reference member, since we have not provided the copy
constructor with any other ABC object that the newly created abcRef
member could be made to reference. We are able to copy the const
member in this case since the newly created const member has yet to be
initialized. As hinted in the code comments above, the copy
constructor supplied above is not necessary, since the synthesized copy
constructor will effectively do exactly the same thing.

If you would like to read further discussion of the Rule of Three, read
the following on-line article (Warning: includes features of the C++
language not discussed in this course):

http://www.artima.com/cppsource/bigtwo.html

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-27-2006
* blangela:
> 3.0 Advanced Topic Addendum
>
> There are a few cases where the C++ compiler cannot provide an
> overloaded assignment operator for your class. If your class contains
> a const member or/and a reference member, the compiler will not be able
> to synthesize an assignment operator for your class. It actually helps
> to think of a reference member as a const member (since it cannot be
> made to reference any other object once it has been initialized).
>
> Interestingly, the compiler will be able to synthesize a copy
> constructor for a class that defines a const member or a reference
> member, but the compiler supplied copy constructor will do a shallow
> copy of any reference members that are defined for the class.
>
> In the case where your class contains a reference member, you will
> likely want to overload the assignment operator for your class (since
> the compiler cannot do it for you).


No, I will not likely want to overload the assignment operator.

The idea that assignment "must" be provided for every class is an
unfortunate misunderstanding that is the cause of an inordinate number
of man-years of maintenance work, because it really messes things up.

Teach your students about the difference between a class meant to be
copyable, and a class meant to be non-copyable -- and don't do that as
an "advanced" topic, because it's really a /fundamental/ topic.

It's generally a good idea to limit a non-copyable class (e.g. a Windows
class) to dynamic allocation, so that it will not pass compilation to
declare an automatic variable, static variable or member of that class.
That can be achieved by limiting access to constructors (with factory
functions provided) or the destructor (no factory functions needed). I
favor the destructor, but unfortunately it's not yet in the FAQ.

There is a discussion of this and associated techniques in <url:
http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf>.

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deep versus Shallow Copy - Part 1 blangela C++ 13 11-27-2006 06:36 PM
Deep versus Shallow Copy - Part 2 blangela C++ 9 11-27-2006 03:35 PM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
deep/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM



Advertisments