Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Use of copy constructor in pass by value

Reply
Thread Tools

Use of copy constructor in pass by value

 
 
madhukar_bm
Guest
Posts: n/a
 
      02-05-2004
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
 
Reply With Quote
 
 
 
 
lilburne
Guest
Posts: n/a
 
      02-05-2004


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.

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      02-05-2004
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.


 
Reply With Quote
 
Risto Lankinen
Guest
Posts: n/a
 
      02-05-2004

"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:bvt7vu$9o6$00$(E-Mail Removed)-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 -



 
Reply With Quote
 
lilburne
Guest
Posts: n/a
 
      02-05-2004


Risto Lankinen wrote:

> "Rolf Magnus" <(E-Mail Removed)> wrote in message
> news:bvt7vu$9o6$00$(E-Mail Removed)-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&);




 
Reply With Quote
 
Risto Lankinen
Guest
Posts: n/a
 
      02-05-2004

"lilburne" <(E-Mail Removed)> wrote in message
news:bvtfvg$109sv0$(E-Mail Removed)-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 -



 
Reply With Quote
 
lilburne
Guest
Posts: n/a
 
      02-05-2004


Risto Lankinen wrote:

> "lilburne" <(E-Mail Removed)> wrote in message
> news:bvtfvg$109sv0$(E-Mail Removed)-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);




 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      02-05-2004
Risto Lankinen wrote:
>
> "Rolf Magnus" <(E-Mail Removed)> wrote in message
> news:bvt7vu$9o6$00$(E-Mail Removed)-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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass by value on with a copy constructor? Marcel Müller C++ 9 11-10-2012 09:45 AM
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments