473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

why '&' sign is used in copy constructor?

just know the reason behind it...
Oct 18 '10 #1
1 1642
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

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

Similar topics

3
by: Naren | last post by:
Hello All, I get an unexpected behaviour for this class D{ /*something*/ }; class B
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
7
by: madhukar_bm | last post by:
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...
3
by: ccs | last post by:
In Meyers' book he gave an example of "virtual copy constructor", which is quite different to an "ordinary" copy constructor by: 1. it returns a pointer to an object instead of a reference. 2. it...
1
by: Adam | last post by:
Why does this compile: if (err != Socket::errSuccess) { switch (err) { case Socket::errTimeout: ConnectionIOException cio(new string(myTCPStream->getErrorString()),...
10
by: ambar.shome | last post by:
Dear All, Whats the difference between a copy constructor and assignment operator. We can assign the values of member variables of one object to another object of same type using both of them....
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
1
by: xqxu.pzhou | last post by:
I wrote a simple allocator "myAlloc" under the g++ 3.2.3. When it is used by Vector, it works well. But when it is used by List, the codes have errors when compling. the error message is: "no...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.