Connecting Tech Pros Worldwide Forums | Help | Site Map

copy constructor

Rahul
Guest
 
Posts: n/a
#1: Nov 25 '07
Hi Everyone,

It is well known that the input parameter which is passed to the
copy constructor is passed as reference and not as as object. Because
passing an object is as good as making another copy which in itself
needs a copy constructor.

However i was wondering why can't the existing object be passed as
a pointer instead of a reference to the copy constructor which creates
a new object.

Thanks in advance!!!

Kai-Uwe Bux
Guest
 
Posts: n/a
#2: Nov 25 '07

re: copy constructor


Rahul wrote:
Quote:
Hi Everyone,
>
It is well known that the input parameter which is passed to the
copy constructor is passed as reference and not as as object. Because
passing an object is as good as making another copy which in itself
needs a copy constructor.
>
However i was wondering why can't the existing object be passed as
a pointer instead of a reference to the copy constructor which creates
a new object.
a) You _can_ define a constructor

T ( T* ptr )
: member_1 ( ptr->member_1 )
, ...
{}

Although it will buy you nothing but trouble since you invite all the perils
of undefined behavior upon you, should you pass a null pointer.


b) Such a constructor is _not_ a copy constructor. The standard defines copy
constructors as those that conform to certain signatures. Also, the
standard says that at certain points copy constructors will be used, say,
to create temporaries. That the constructor in (a) is not a copy
constructor has therefore the consequence that it will not be considered
for such purposes.


That the copy-constructor is invoked automatically in certain contexts and
can be optimized away in others creates a restriction on its semantics: it
really better just construct a copy of the object. Thus, one problem with
making (a) a valid signature for copy-constructors is that it renders it
impossible to use a constructor like (a) for other purposes. E.g. a
single-linked list might have a private constructor

List ( List* tail_ptr = 0, ValueType const & value = ValueType() )


Best

Kai-Uwe Bux
=?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?=
Guest
 
Posts: n/a
#3: Nov 25 '07

re: copy constructor


Rahul:
Quote:
It is well known that the input parameter which is passed to the
copy constructor is passed as reference and not as as object. Because
passing an object is as good as making another copy which in itself
needs a copy constructor.

The terminology you're looking for is "pass by value" Vs "pass by
reference".

By value: void Func(MyClass obj); /* New object created within the
function */

By reference: void Func(MyClass &obj); /* Same external object */

--
Tomás Ó hÉilidhe
Rahul
Guest
 
Posts: n/a
#4: Nov 26 '07

re: copy constructor


On Nov 25, 10:34 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Quote:
Rahul wrote:
Quote:
Hi Everyone,
>
Quote:
It is well known that the input parameter which is passed to the
copy constructor is passed as reference and not as as object. Because
passing an object is as good as making another copy which in itself
needs a copy constructor.
>
Quote:
However i was wondering why can't the existing object be passed as
a pointer instead of a reference to the copy constructor which creates
a new object.
>
a) You _can_ define a constructor
>
T ( T* ptr )
: member_1 ( ptr->member_1 )
, ...
{}
>
Although it will buy you nothing but trouble since you invite all the perils
of undefined behavior upon you, should you pass a null pointer.
>
b) Such a constructor is _not_ a copy constructor. The standard defines copy
constructors as those that conform to certain signatures. Also, the
standard says that at certain points copy constructors will be used, say,
to create temporaries. That the constructor in (a) is not a copy
constructor has therefore the consequence that it will not be considered
for such purposes.
>
Yes, it wouldn't be called as that wouldn't be a copy
constructor as per current c++ implementation and standards. Howevre,
my question was at the design level in c++ as to why reference was
decided to pass to the copy constructor and why wasn't a pointer
considered?
Quote:
That the copy-constructor is invoked automatically in certain contexts and
can be optimized away in others creates a restriction on its semantics: it
really better just construct a copy of the object. Thus, one problem with
making (a) a valid signature for copy-constructors is that it renders it
impossible to use a constructor like (a) for other purposes. E.g. a
single-linked list might have a private constructor
>
List ( List* tail_ptr = 0, ValueType const & value = ValueType() )
>
Best
>
Kai-Uwe Bux
Kai-Uwe Bux
Guest
 
Posts: n/a
#5: Nov 26 '07

re: copy constructor


Rahul wrote:
Quote:
On Nov 25, 10:34 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Quote:
>Rahul wrote:
Quote:
Hi Everyone,
>>
Quote:
It is well known that the input parameter which is passed to the
copy constructor is passed as reference and not as as object. Because
passing an object is as good as making another copy which in itself
needs a copy constructor.
>>
Quote:
However i was wondering why can't the existing object be passed as
a pointer instead of a reference to the copy constructor which creates
a new object.
>>
>a) You _can_ define a constructor
>>
> T ( T* ptr )
> : member_1 ( ptr->member_1 )
> , ...
> {}
>>
>Although it will buy you nothing but trouble since you invite all the
>perils of undefined behavior upon you, should you pass a null pointer.
>>
>b) Such a constructor is _not_ a copy constructor. The standard defines
>copy constructors as those that conform to certain signatures. Also, the
>standard says that at certain points copy constructors will be used, say,
>to create temporaries. That the constructor in (a) is not a copy
>constructor has therefore the consequence that it will not be considered
>for such purposes.
>>
>
Yes, it wouldn't be called as that wouldn't be a copy
constructor as per current c++ implementation and standards. Howevre,
my question was at the design level in c++ as to why reference was
decided to pass to the copy constructor and why wasn't a pointer
considered?
Please read the remainder of my previous post. It addresses exactly that
question.
Quote:
Quote:
>That the copy-constructor is invoked automatically in certain contexts
>and can be optimized away in others creates a restriction on its
>semantics: it really better just construct a copy of the object. Thus,
>one problem with making (a) a valid signature for copy-constructors is
>that it renders it impossible to use a constructor like (a) for other
>purposes. E.g. a single-linked list might have a private constructor
>>
> List ( List* tail_ptr = 0, ValueType const & value = ValueType() )
>>
[snip]


Best

Kai-Uwe Bux

Closed Thread