copy ctor

S

subramanian100in

I thought the copy ctor always takes the form
Test::Test(const Test & arg);
for a class Test.

But I read the following sentence in Stanley Lippman's C++ Primer 4th
Edition(Page 476):

The copy constructor is a special constructor that has a single
parameter that is a (usually const) reference to the class type.

My doubt
--------------
Since the above sentence contains "(usually const)" in parantheses,
does it mean that we may need a copy ctor that takes the form
Test::Test(Test & arg) ?
(that is, reference to plain type) ?

Kindly let me know when we will need this second form.

Thanks
V.Subramanian
 
G

Guest

I thought the copy ctor always takes the form
Test::Test(const Test & arg);
for a class Test.

But I read the following sentence in Stanley Lippman's C++ Primer 4th
Edition(Page 476):

The copy constructor is a special constructor that has a single
parameter that is a (usually const) reference to the class type.

My doubt
--------------
Since the above sentence contains "(usually const)" in parantheses,
does it mean that we may need a copy ctor that takes the form
Test::Test(Test & arg) ?
(that is, reference to plain type) ?

Kindly let me know when we will need this second form.

Yes, you might have a need for a non-const copy constructor, but I can
frankly not come up with a situation where you would. Perhaps if you
want to have special copy construction for some argument types (const
and temporaries), then you could declare both a const and a non-const
copy constructor.

Please also not that the copy constructor does not have to take only one
argument, as long as there are default values for the other arguments.
 
K

Kai-Uwe Bux

Erik said:
Yes, you might have a need for a non-const copy constructor, but I can
frankly not come up with a situation where you would. Perhaps if you
want to have special copy construction for some argument types (const
and temporaries), then you could declare both a const and a non-const
copy constructor.

std::auto_ptr<> is an example. It has a non-const copy constructure since
copying involves transfer of ownership and changes the source object.

[snip]


Best

Kai-Uwe Bux
 
J

James Kanze

[...]
std::auto_ptr<> is an example. It has a non-const copy
constructure since copying involves transfer of ownership and
changes the source object.

And look at all the hoops std::auto_ptr has to jump through so
you can e.g. return it from a function.
 

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
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top