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

why '&' sign is used in copy constructor?

P: n/a
just know the reason behind it...
Oct 18 '10 #1
Share this Question
Share on Google+
1 Reply

P: 2
The & sign means 'reference':
const X&
means reference to the object of class X.
If & is not used then the copy is taken. In the copy constructor you define how the copy should be taken. If you declare a function:
void f(X a) ...
it means that the argument should always be copied (even if it is a variable of class X).
If you declare a function as:
void f(const X& a)
then if the argument is a variable of class X it won't be copied: the refrence to it will be passed to the parameter. That means that the second declaration will be more efficient. But both declarations can be used to pass any expressions of class X.

You cannot use (X a) or (const X a) in a copy construtor,
definition, it will mean that the copy should be created in copy constrcutor itself: its a recursive call.
Oct 18 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.