Connecting Tech Pros Worldwide Help | Site Map

copy ctor

  #1  
Old September 25th, 2007, 03:45 PM
subramanian100in@yahoo.com, India
Guest
 
Posts: n/a
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

  #2  
Old September 25th, 2007, 05:35 PM
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a

re: copy ctor


On 2007-09-25 16:44, subramanian100in@yahoo.com, India wrote:
Quote:
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.

--
Erik Wikström
  #3  
Old September 25th, 2007, 08:35 PM
Kai-Uwe Bux
Guest
 
Posts: n/a

re: copy ctor


Erik Wikström wrote:
Quote:
On 2007-09-25 16:44, subramanian100in@yahoo.com, India wrote:
Quote:
>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.
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

  #4  
Old September 26th, 2007, 09:55 AM
James Kanze
Guest
 
Posts: n/a

re: copy ctor


On Sep 25, 9:15 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:

[...]
Quote:
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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy ctor vs default ctor Subramanian India answers 1 August 16th, 2007 05:18 PM
copy ctor vs default ctor subramanian100in@yahoo.com, India answers 2 August 15th, 2007 11:55 AM
static array initialization and private copy ctor John Salmon answers 3 December 11th, 2006 01:15 PM
A simple copy ctor + inheritance qn mescaline answers 3 July 22nd, 2005 05:38 AM
Templated Copy Ctor Rich answers 1 July 22nd, 2005 04:55 AM