uses of the copy constructor

J

jimjim

Hello all,

What other reason is there for using the copy constructor apart from copying
data members which are pointers to dynamically allocated objects?

TIA
 
M

Mike Wahler

jimjim said:
Hello all,

What other reason is there for using the copy constructor apart from
copying data members which are pointers to dynamically allocated objects?

The sole purpose of a copy constructor is to initialize a
newly created object with a copy of the value of another,
already existing object of the same type. If you don't define
one, the compiler will synthesize one for you (which might or
might not do what is needed). So there's no way to 'not use'
a copy constructor. (One might want to prohibit copying;
this is done by making the copy ctor private, but it's
still there (at least semantically; an optimizer might elude
the code if possible, but the program is still required to
behave 'as if' the code were there.)

In the cases where default copy constructor's behavior
(memberwise assignment) is inappropriate, one must write
a custom copy constructor. Your example of a 'deep copy'
is very common, but not the only reason for writing one.
Other examples include anything which cannot be done with
memberwise assignment. The list is theoretically unlimited,
including 'perverse' things that make the operation not really
a 'copy', but probably the most common general theme is 'resource
acquisition and disposition'. Look up Stroustroup's acronym
"RAII" (Resource Acquisition is Initialization).

-Mike
 
M

Mike Wahler

Mike Wahler said:
this is done by making the copy ctor private, but it's
still there (at least semantically; an optimizer might elude

elide

Ack! :)

-Mike
 
J

Jim Langston

jimjim said:
Hello all,

What other reason is there for using the copy constructor apart from
copying data members which are pointers to dynamically allocated objects?

I could think of one other case, perhaps your class opens a file for output.
You wouldn't want the same file opened twice for output (don't think the
system would let you anyway) so you would either need to open a totally
different file, or somehow write though the other class object or something.
Actually, that'd probably be an interesting problem to get around.
 
J

jimjim

The list is theoretically unlimited, including 'perverse' things
that make the operation not really a 'copy'

Can you give me an example of what you mean, please?

TIA
 
M

Mike Wahler

jimjim said:
Can you give me an example of what you mean, please?

class C
{
int i;
int j;

public:
C() : i(0), j(0) {}
C(const C& rhs) : i(rhs.j), j(rhs.i) {}
};


The copy constructor swaps the values of 'i' and 'j'
members in the constructed object. So the result
isn't a 'copy' of object 'rhs', although the
constructor meets language requirments for being
a 'copy constructor'.

Other possibilities include initializing 'i' and 'j'
to some other unrelated values, or not initializing
them at all. Even more perverse would be 'const_casting'
rhs, and modifying that.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top