copy ctor semantics

P

puzzlecracker

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
 
D

David Côme

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 ?
 
K

Kai-Uwe Bux

puzzlecracker said:
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
 

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
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top