By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,619 Members | 1,534 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,619 IT Pros & Developers. It's quick & easy.

Use of copy constructor in pass by value

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a


madhukar_bm wrote:
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.

Jul 22 '05 #2

P: n/a
madhukar_bm wrote:
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.
Jul 22 '05 #3

P: n/a

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bv*************@news.t-online.com...
madhukar_bm wrote:
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.


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 -

Jul 22 '05 #4

P: n/a


Risto Lankinen wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bv*************@news.t-online.com...

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?


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&);


Jul 22 '05 #5

P: n/a

"lilburne" <li******@godzilla.com> wrote in message
news:bv*************@ID-179504.news.uni-berlin.de...

Risto Lankinen wrote:
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.


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

- Risto -

Jul 22 '05 #6

P: n/a


Risto Lankinen wrote:
"lilburne" <li******@godzilla.com> wrote in message
news:bv*************@ID-179504.news.uni-berlin.de...
Risto Lankinen wrote:

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.

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);


Jul 22 '05 #7

P: n/a
Risto Lankinen wrote:

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bv*************@news.t-online.com...
madhukar_bm wrote:
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.


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).

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.