Deep versus Shallow Copy - Part 3

B

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).

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::eek: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
 
A

Alf P. Steinbach

* 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top