<s>
In	O
the	O
C++	B-Language
programming	I-Language
language	I-Language
,	O
a	O
copy	B-Language
constructor	I-Language
is	O
a	O
special	O
constructor	O
for	O
creating	O
a	O
new	O
object	O
as	O
a	O
copy	O
of	O
an	O
existing	O
object	O
.	O
</s>
<s>
Copy	B-Language
constructors	I-Language
are	O
the	O
standard	O
way	O
of	O
copying	O
objects	O
in	O
C++	B-Language
,	O
as	O
opposed	O
to	O
cloning	B-Application
,	O
and	O
have	O
C++	B-Language
-specific	O
nuances	O
.	O
</s>
<s>
The	O
first	O
argument	O
of	O
such	O
a	O
constructor	O
is	O
a	O
reference	B-Language
to	O
an	O
object	O
of	O
the	O
same	O
type	O
as	O
is	O
being	O
constructed	O
(	O
const	O
or	O
non-const	O
)	O
,	O
which	O
might	O
be	O
followed	O
by	O
parameters	O
of	O
any	O
type	O
(	O
all	O
having	O
default	O
values	O
)	O
.	O
</s>
<s>
Normally	O
the	O
compiler	B-Language
automatically	O
creates	O
a	O
copy	B-Language
constructor	I-Language
for	O
each	O
class	O
(	O
known	O
as	O
an	O
implicit	O
copy	B-Language
constructor	I-Language
)	O
but	O
for	O
special	O
cases	O
the	O
programmer	B-Application
creates	O
the	O
copy	B-Language
constructor	I-Language
,	O
known	O
as	O
a	O
user-defined	O
copy	B-Language
constructor	I-Language
.	O
</s>
<s>
In	O
such	O
cases	O
,	O
the	O
compiler	B-Language
does	O
not	O
create	O
one	O
.	O
</s>
<s>
Hence	O
,	O
there	O
is	O
always	O
one	O
copy	B-Language
constructor	I-Language
that	O
is	O
either	O
defined	O
by	O
the	O
user	O
or	O
by	O
the	O
system	O
.	O
</s>
<s>
A	O
user-defined	O
copy	B-Language
constructor	I-Language
is	O
generally	O
needed	O
when	O
an	O
object	O
owns	O
pointers	O
or	O
non-shareable	O
references	O
,	O
such	O
as	O
to	O
a	O
file	B-Operating_System
,	O
in	O
which	O
case	O
a	O
destructor	B-Language
and	O
an	O
assignment	O
operator	O
should	O
also	O
be	O
written	O
(	O
see	O
Rule	O
of	O
three	O
)	O
.	O
</s>
<s>
Copying	O
of	O
objects	O
is	O
achieved	O
by	O
the	O
use	O
of	O
a	O
copy	B-Language
constructor	I-Language
and	O
an	O
assignment	O
operator	O
.	O
</s>
<s>
A	O
copy	B-Language
constructor	I-Language
has	O
as	O
its	O
first	O
parameter	O
a	O
(	O
possibly	O
const	O
or	O
volatile	B-Operating_System
)	O
reference	B-Language
to	O
its	O
own	O
class	O
type	O
.	O
</s>
<s>
The	O
following	O
would	O
be	O
valid	O
copy	B-Language
constructors	I-Language
for	O
class	O
X	O
:	O
</s>
<s>
The	O
X&	O
form	O
of	O
the	O
copy	B-Language
constructor	I-Language
is	O
used	O
when	O
it	O
is	O
necessary	O
to	O
modify	O
the	O
copied	O
object	O
.	O
</s>
<s>
A	O
reference	B-Language
must	O
be	O
provided	O
:	O
</s>
<s>
The	O
following	O
are	O
invalid	O
copy	B-Language
constructors	I-Language
because	O
copy_from_me	O
is	O
not	O
passed	O
as	O
reference	B-Language
( &	O
)	O
:	O
</s>
<s>
The	O
following	O
cases	O
may	O
result	O
in	O
a	O
call	O
to	O
a	O
copy	B-Language
constructor	I-Language
:	O
</s>
<s>
It	O
is	O
however	O
,	O
not	O
guaranteed	O
that	O
a	O
copy	B-Language
constructor	I-Language
will	O
be	O
called	O
in	O
these	O
cases	O
,	O
because	O
the	O
C++	B-Language
Standard	O
allows	O
the	O
compiler	B-Language
to	O
optimize	O
the	O
copy	O
away	O
in	O
certain	O
cases	O
,	O
one	O
example	O
being	O
the	O
return	O
value	O
optimization	O
(	O
sometimes	O
referred	O
to	O
as	O
RVO	O
)	O
.	O
</s>
<s>
The	O
copy	B-Language
constructor	I-Language
is	O
used	O
only	O
for	O
initializations	O
,	O
and	O
does	O
not	O
apply	O
to	O
assignments	O
where	O
the	O
assignment	O
operator	O
is	O
used	O
instead	O
.	O
</s>
<s>
The	O
implicit	O
copy	B-Language
constructor	I-Language
of	O
a	O
class	O
calls	O
base	O
copy	B-Language
constructors	I-Language
and	O
copies	O
its	O
members	O
by	O
means	O
appropriate	O
to	O
their	O
type	O
.	O
</s>
<s>
If	O
it	O
is	O
a	O
class	O
type	O
,	O
the	O
copy	B-Language
constructor	I-Language
is	O
called	O
.	O
</s>
<s>
By	O
using	O
a	O
user-defined	O
copy	B-Language
constructor	I-Language
the	O
programmer	B-Application
can	O
define	O
the	O
behavior	O
to	O
be	O
performed	O
when	O
an	O
object	O
is	O
copied	O
.	O
</s>
<s>
These	O
examples	O
illustrate	O
how	O
copy	B-Language
constructors	I-Language
work	O
and	O
why	O
they	O
are	O
sometimes	O
required	O
.	O
</s>
<s>
The	O
compiler	B-Language
has	O
generated	O
a	O
copy	B-Language
constructor	I-Language
for	O
us	O
,	O
and	O
it	O
could	O
be	O
written	O
like	O
this	O
:	O
</s>
<s>
So	O
,	O
when	O
do	O
we	O
really	O
need	O
a	O
user-defined	O
copy	B-Language
constructor	I-Language
?	O
</s>
<s>
Consider	O
a	O
very	O
simple	O
dynamic	B-Data_Structure
array	I-Data_Structure
class	O
like	O
the	O
following	O
:	O
</s>
<s>
Since	O
we	O
did	O
not	O
specify	O
a	O
copy	B-Language
constructor	I-Language
,	O
the	O
compiler	B-Language
generated	O
one	O
for	O
us	O
.	O
</s>
<s>
When	O
the	O
program	O
reaches	O
line	O
(	O
1	O
)	O
,	O
copy	O
's	O
destructor	B-Language
gets	O
called	O
(	O
because	O
objects	O
on	O
the	O
stack	O
are	O
destroyed	O
automatically	O
when	O
their	O
scope	O
ends	O
)	O
.	O
</s>
<s>
Array	O
's	O
destructor	B-Language
deletes	O
the	O
data	O
array	O
of	O
the	O
original	O
,	O
therefore	O
,	O
when	O
it	O
deleted	O
copy	O
's	O
data	O
,	O
because	O
they	O
share	O
the	O
same	O
pointer	O
,	O
it	O
also	O
deleted	O
first	O
's	O
data	O
.	O
</s>
<s>
This	O
produces	O
a	O
segmentation	B-Error_Name
fault	I-Error_Name
.	O
</s>
<s>
If	O
we	O
write	O
our	O
own	O
copy	B-Language
constructor	I-Language
that	O
performs	O
a	O
deep	O
copy	O
then	O
this	O
problem	O
goes	O
away	O
.	O
</s>
<s>
Now	O
,	O
other	O
's	O
destructor	B-Language
deletes	O
only	O
its	O
data	O
,	O
and	O
not	O
first	O
's	O
data	O
.	O
</s>
<s>
Line	O
(	O
2	O
)	O
will	O
not	O
produce	O
a	O
segmentation	B-Error_Name
fault	I-Error_Name
anymore	O
.	O
</s>
<s>
The	O
copy-on-write	B-Language
strategy	O
makes	O
a	O
copy	O
of	O
the	O
data	O
only	O
when	O
it	O
is	O
written	O
to	O
.	O
</s>
<s>
Reference	B-General_Concept
counting	I-General_Concept
keeps	O
the	O
count	O
of	O
how	O
many	O
objects	O
are	O
referencing	O
the	O
data	O
,	O
and	O
will	O
delete	O
it	O
only	O
when	O
this	O
count	O
reaches	O
zero	O
(	O
e.g.	O
</s>
<s>
There	O
is	O
no	O
such	O
thing	O
as	O
"	O
bitwise	O
copy	B-Language
constructor	I-Language
"	O
in	O
C++	B-Language
.	O
</s>
<s>
However	O
,	O
the	O
default	O
generated	O
copy	B-Language
constructor	I-Language
copies	O
by	O
invoking	O
copy	B-Language
constructors	I-Language
on	O
members	O
,	O
and	O
for	O
a	O
raw	O
pointer	O
member	O
this	O
will	O
copy	O
the	O
raw	O
pointer	O
(	O
i.e.	O
</s>
<s>
A	O
logical	O
copy	B-Language
constructor	I-Language
makes	O
a	O
true	O
copy	O
of	O
the	O
structure	O
as	O
well	O
as	O
its	O
dynamic	O
structures	O
.	O
</s>
<s>
Logical	O
copy	B-Language
constructors	I-Language
come	O
into	O
the	O
picture	O
mainly	O
when	O
there	O
are	O
pointers	O
or	O
complex	O
objects	O
within	O
the	O
object	O
being	O
copied	O
.	O
</s>
<s>
An	O
explicit	O
copy	B-Language
constructor	I-Language
is	O
one	O
that	O
is	O
declared	O
explicit	O
by	O
using	O
the	O
explicit	O
keyword	O
.	O
</s>
