Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Use of copy constructor in pass by value (http://www.velocityreviews.com/forums/t281147-use-of-copy-constructor-in-pass-by-value.html)

madhukar_bm 02-05-2004 10:49 AM

Use of copy constructor in pass by value
 
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

lilburne 02-05-2004 10:53 AM

Re: Use of copy constructor in pass by value
 


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.


Rolf Magnus 02-05-2004 10:55 AM

Re: Use of copy constructor in pass by value
 
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.



Risto Lankinen 02-05-2004 01:08 PM

Re: Use of copy constructor in pass by value
 

"Rolf Magnus" <ramagnus@t-online.de> wrote in message
news:bvt7vu$9o6$00$1@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 -




lilburne 02-05-2004 01:20 PM

Re: Use of copy constructor in pass by value
 


Risto Lankinen wrote:

> "Rolf Magnus" <ramagnus@t-online.de> wrote in message
> news:bvt7vu$9o6$00$1@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&);





Risto Lankinen 02-05-2004 01:44 PM

Re: Use of copy constructor in pass by value
 

"lilburne" <lilburne@godzilla.com> wrote in message
news:bvtfvg$109sv0$1@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 -




lilburne 02-05-2004 02:43 PM

Re: Use of copy constructor in pass by value
 


Risto Lankinen wrote:

> "lilburne" <lilburne@godzilla.com> wrote in message
> news:bvtfvg$109sv0$1@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);





Karl Heinz Buchegger 02-05-2004 02:43 PM

Re: Use of copy constructor in pass by value
 
Risto Lankinen wrote:
>
> "Rolf Magnus" <ramagnus@t-online.de> wrote in message
> news:bvt7vu$9o6$00$1@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
kbuchegg@gascad.at


All times are GMT. The time now is 04:42 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.