B
blangela
2.0 Sample Code
class ABC // dummy class used below
{};
class Example2
{
public:
Example2(); // default ctor
Example2( const Example2 &); // copy ctor
Example2 & operator = (const Example2 &); // overloaded
// assignment operator
~Example2(); //dtor
private:
int ii;
double dd;
ABC * abcPtr; // will point to a dynamically created ABC object
};
// default ctor
Example2::Example2()
{
abcPtr = new ABC; // dynamically create an ABC object and have
// the class member abcPtr point to it.
ii = 10; // assign some value
dd = 3.765; // assign some value
}
// copy ctor
Example2::Example2(const Example2 & src)
:ii(src.ii),dd(src.dd) // MIL
{
abcPtr = new ABC; // Same as default ctor above.
*abcPtr = *(src.abcPtr); // Do a deep copy.
}
// dtor
Example2::~Example2()
{
delete abcPtr; //Destroy the dynamically created ABC object.
abcPtr = 0; //Not necessary, but prevents a calamity if an
// attempt is made to destroy the same
// ABC object a second time.
}
// overloaded assignement operator
Example2 & Example2:
perator = (const Example2 & RHS)
{
//First check that the operand on the Right Hand Side (RHS)
// of the assignment operator is not the exact same object
// as the operand on the LHS of the assignment operator.
// We do this by testing to see if both objects have the
// same address!
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
*abcPtr = *(RHS.abcPtr); // here we need a deep copy.
return *this; // return the modified LHS operand.
}
}
// Simple program to test our Example2 class
int main()
{
Example2 ex2_1, ex2_2; // Will invoke default ctor (twice).
ex2_1 = ex2_2; // Will invoke overloaded Assignment operator.
Example2 ex2_3(ex2_1); // Will invoke copy ctor.
return 0; // We are finished!
} // end of main() - Example2 dtor will be invoked 3 times
class ABC // dummy class used below
{};
class Example2
{
public:
Example2(); // default ctor
Example2( const Example2 &); // copy ctor
Example2 & operator = (const Example2 &); // overloaded
// assignment operator
~Example2(); //dtor
private:
int ii;
double dd;
ABC * abcPtr; // will point to a dynamically created ABC object
};
// default ctor
Example2::Example2()
{
abcPtr = new ABC; // dynamically create an ABC object and have
// the class member abcPtr point to it.
ii = 10; // assign some value
dd = 3.765; // assign some value
}
// copy ctor
Example2::Example2(const Example2 & src)
:ii(src.ii),dd(src.dd) // MIL
{
abcPtr = new ABC; // Same as default ctor above.
*abcPtr = *(src.abcPtr); // Do a deep copy.
}
// dtor
Example2::~Example2()
{
delete abcPtr; //Destroy the dynamically created ABC object.
abcPtr = 0; //Not necessary, but prevents a calamity if an
// attempt is made to destroy the same
// ABC object a second time.
}
// overloaded assignement operator
Example2 & Example2:
{
//First check that the operand on the Right Hand Side (RHS)
// of the assignment operator is not the exact same object
// as the operand on the LHS of the assignment operator.
// We do this by testing to see if both objects have the
// same address!
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
*abcPtr = *(RHS.abcPtr); // here we need a deep copy.
return *this; // return the modified LHS operand.
}
}
// Simple program to test our Example2 class
int main()
{
Example2 ex2_1, ex2_2; // Will invoke default ctor (twice).
ex2_1 = ex2_2; // Will invoke overloaded Assignment operator.
Example2 ex2_3(ex2_1); // Will invoke copy ctor.
return 0; // We are finished!
} // end of main() - Example2 dtor will be invoked 3 times