Copy constructors are called on some occasions in C++. One of them is when you have a function like
void f(CExample obj)
{
// ...
}
In this case, when you make a call
CExample x;
f( x );
CExample::CExample
gets called to construct obj
from x
.
In case you have the following signature
void f(CExample &obj)
{
// ...
}
(note that obj
is now passed by reference), the copy constructor CExample::CExample
does not get called.
In the case your constructor accepts the object to be copied by value (as with the function f
in the first example), compiler will have to call the copy constructor first in order to create a copy (as with the function f
, again), but... oops, we have to call the copy constructor in order to call the copy constructor. This sounds bad, doesn't it?