Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > copy ctor semantics

Reply
Thread Tools

copy ctor semantics

 
 
puzzlecracker
Guest
Posts: n/a
 
      04-20-2008
Why should the copy constructor take a const reference and not just a
const?

If efficiency is no longer an issue, say size of the object won't
noticeably degrade performance, is it because we can assign temp
object only to const reference, and not just const?


class string {
public;
string();
operator char *();
string (const string & )

};


Say, we have string s="hello";

wouldn't that work if copy ctor took const?

I just don't see any reasons other than efficiency.

Thanks
 
Reply With Quote
 
 
 
 
David Côme
Guest
Posts: n/a
 
      04-20-2008
On Sun, 20 Apr 2008 18:43:54 +0200, puzzlecracker <(E-Mail Removed)>
wrote:

> Why should the copy constructor take a const reference and not just a
> const?
>
> If efficiency is no longer an issue, say size of the object won't
> noticeably degrade performance, is it because we can assign temp
> object only to const reference, and not just const?
>
>
> class string {
> public;
> string();
> operator char *();
> string (const string & )
>
> };
>
>
> Say, we have string s="hello";
>
> wouldn't that work if copy ctor took const?
>
> I just don't see any reasons other than efficiency.
>
> Thanks



Why copy ctor should be allowed to change the element passed at him ?
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      04-20-2008
puzzlecracker wrote:

> Why should the copy constructor take a const reference and not just a
> const?

[snip]

Given a function with signature

maybe_void foo ( A const a );

calling foo(a) will call the copy constructor to create a temporary of of
the parameter for the function to work on. The function will not be allowed
to modify the temporary.

Therefore, foo(a) invokes the copy constructor for A.

Question: what happens if you had a copy constructor with signature:

A ( A const a );


Best

Kai-Uwe Bux
 
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
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
copy ctor vs default ctor subramanian100in@yahoo.com, India C++ 2 08-15-2007 10:49 AM
ctor/dtor calls and ctor init seq Grizlyk C++ 8 11-29-2006 06:35 AM
Templates, copy ctor and type-conversion ctor NVH C++ 8 07-06-2006 07:19 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM



Advertisments