By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,915 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,915 IT Pros & Developers. It's quick & easy.

Copy constructor

P: n/a
Hi,

Can someone please explain what a copy constructor does and how it's
useful? Thanks,

Terry

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Terry" <te***@nospam.com> wrote in message
news:3f********@clarion.carno.net.au...
Hi,

Can someone please explain what a copy constructor does and how it's
useful? Thanks,

It constructs an object (like the regular c'tor) from an already
existing object. This way you avoid first constructing and then assigning
(ie. two steps, instead of one).

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #2

P: n/a
A

Hi,

Can someone please explain what a copy constructor does and how it's
useful? Thanks,

It constructs an object (like the regular c'tor) from an already
existing object. This way you avoid first constructing and then assigning
(ie. two steps, instead of one).


can't you use use the assignment operator to copy? that should be one step
as well.

asasas


Jul 19 '05 #3

P: n/a
"A" <A@iprimus.com.au> wrote...

Hi,

Can someone please explain what a copy constructor does and how it's
useful? Thanks,

It constructs an object (like the regular c'tor) from an already
existing object. This way you avoid first constructing and then assigning (ie. two steps, instead of one).


can't you use use the assignment operator to copy? that should be one step
as well.


No. Assignment operator is useless when passing parameters by
value or returning by value is concerned. Those are the main
uses of the copy constructor.

Victor
Jul 19 '05 #4

P: n/a

"A" <A@iprimus.com.au> wrote in message
news:3f********@news.iprimus.com.au...

Hi,

Can someone please explain what a copy constructor does and how it's
useful? Thanks,

It constructs an object (like the regular c'tor) from an already
existing object. This way you avoid first constructing and then assigning (ie. two steps, instead of one).


can't you use use the assignment operator to copy? that should be one step
as well.

asasas


A a;
A b(a); // copy construct, one step
A c; // default construct, step 1
c = a; // assign, step 2

Creating a copy of an object using assignment uses two steps, first the
object is constructed, then it is assigned.

john
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.