copy constructor

R

Rahul

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!!!
 
K

Kai-Uwe Bux

Rahul said:
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
 
T

Tomás Ó hÉilidhe

Rahul:
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 */
 
R

Rahul

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?
 
K

Kai-Uwe Bux

Rahul said:
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.

[snip]


Best

Kai-Uwe Bux
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top