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