Use of copy constructor in pass by value

M

madhukar_bm

We know that copy constructor is used if a class object is passed by
value to a function, is there any logic behind the use of copy
constructor as assignment operator could also have been used instead
of copy constructor
Regards
Madhukar
 
L

lilburne

madhukar_bm said:
We know that copy constructor is used if a class object is passed by
value to a function, is there any logic behind the use of copy
constructor as assignment operator could also have been used instead
of copy constructor

You first have to have an object to assign to it, the copy constructor
creates one.
 
R

Rolf Magnus

madhukar_bm said:
We know that copy constructor is used if a class object is passed by
value to a function, is there any logic behind the use of copy
constructor as assignment operator could also have been used instead
of copy constructor

What would it assign to? You have to create a new object that is a copy
of an existing one, and that's exactly what the copy constructor is
for. The assignment operator is for making an existing object a copy of
another one.
 
R

Risto Lankinen

Rolf Magnus said:
What would it assign to? You have to create a new object that is a copy
of an existing one, and that's exactly what the copy constructor is
for. The assignment operator is for making an existing object a copy of
another one.

This is in fact an interesting question. If copy constructor is
inaccessible, is the system allowed to default-construct, and
then assign, to simulate the effect of copy construction when
it implicitly needs to? If not, would it make sense to allow it
to happen?

- Risto -
 
L

lilburne

Risto said:
This is in fact an interesting question. If copy constructor is
inaccessible, is the system allowed to default-construct, and
then assign, to simulate the effect of copy construction when
it implicitly needs to? If not, would it make sense to allow it
to happen?

NO! NO! NO! NO! NO!

In most of my class I specifically declare the copy ctor and assignment
operator as private, because I do not want anyone EVER writing code that
can create temporary copies.

so in our libraries what would normally be

Polyline(const Polyline&);
Polyline& operator=(const Polyline&);

are declared as:

Polyline(enumCOPY, const Polyline&);
Polyline& become(enumCOPY, const Polyline&);
 
R

Risto Lankinen

lilburne said:
NO! NO! NO! NO! NO!

In most of my class I specifically declare the copy ctor and assignment
operator as private, because I do not want anyone EVER writing code that
can create temporary copies.

Nothing to NO! about, since that would still be prevented
as long as assignment operator is private as well.

- Risto -
 
L

lilburne

Risto said:
Nothing to NO! about, since that would still be prevented
as long as assignment operator is private as well.

Well that does force you to make both private, and making each of the
functions private is done for seperate though related reasons, which
probably means there are times when you do want one without the other.

The private copy-ctor stops people from writing functions like:

void function();

I had a junior colleague come ask me just last week why it was that he
was getting compiler errors in the above case.

The private assignment operator stops people from writing:

myBigExpensiveClass& function(int);

myBigExpensiveClass x;
x = function(10);
 
K

Karl Heinz Buchegger

Risto said:
This is in fact an interesting question. If copy constructor is
inaccessible, is the system allowed to default-construct, and
then assign, to simulate the effect of copy construction when
it implicitly needs to? If not, would it make sense to allow it
to happen?

I opt for no.
If the copy ctor is inaccessible, there is a reason to it
(whatever that reason might be). To allow the compiler to find
a way around that, would circumvent that reason.

(There are already enough problems when the compiler tries
to use conversion operators and constructors to make things
compilable).
 

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
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top