The copy constructor is used to initialise
a new object under construction from
the values of an exixting object.
Example 1;
1 2 3
|
T someT;
T anotherT = someT; //compiler optimisatiow call copy constructor
|
Example 2;
1 2 3
|
T someT;
T anotherT(someT); //copy construct anotherT
|
The copy constructor looks something like this:
1 2 3 4 5
|
T::T(const T& other)
{
//do copying stuff
}
|
So - the this object will be
anotherT and
const T& other parameter will be the
existing object - in this case
someT.
As the
anotherT object is the one being created/initialised
it's values are all nonsense - the whole idea is to intitialise
it from the values of the existing object passed as the function parameter.
So copying values from
anotherT (under construction) to
someT (existing object) doesn't
make sense (and could cause segfaults/memory access violations.)