+ 2

references?

In the following code can someone please explain why they are using references and what exactly they are referencing when they say Const string& name https://code.sololearn.com/cHBTnR1sywn4/#cpp

27th Jun 2017, 3:50 PM
Bryan
11 Answers
+ 3
References are used to avoid copying a whole object which creates an overhead. Instead of making a copy, the address of the object in memory is passed into the function. This also means that any changes you make to that object will be made to the original object, hence you use const when you want to make sure you don't make any chnages to that object if you dont need to.
27th Jun 2017, 3:58 PM
aklex
aklex - avatar
+ 1
You're obviusly speaking about parameter... what you've written means that you're passing a string by address and that the variable you're passing cannot be modified ("const" stand for constant) by the function passing a parameter by address will make the esecution faster but give the chance to the function you're passing the parameter to, to change the variable. that's why you pass it as "const"
27th Jun 2017, 4:02 PM
2_3rr0r5
2_3rr0r5 - avatar
0
refrences are memory address followed by & sign
27th Jun 2017, 3:55 PM
Aakash Sharma
Aakash Sharma - avatar
0
Whoever is downvoting If the answer is wrong. Care to explain, why it is wrong?
27th Jun 2017, 6:52 PM
Manual
Manual - avatar
0
Or do you enjoy downvoting users for fun?
27th Jun 2017, 6:53 PM
Manual
Manual - avatar
- 1
but why is it used in that context
27th Jun 2017, 3:58 PM
Bryan
- 1
I don't see what is actually being referenced
27th Jun 2017, 4:01 PM
Bryan
- 1
Hey Brian, I just made a code with vectors! I will link it later with some resources.
27th Jun 2017, 4:47 PM
Manual
Manual - avatar
- 1
The code looks good!
27th Jun 2017, 4:48 PM
Manual
Manual - avatar
- 1
Here is my Abilities code I made it in order to get better at using vectors. https://code.sololearn.com/cMMcFCTL93kQ
27th Jun 2017, 6:49 PM
Manual
Manual - avatar
- 2
Vector is a class you are using objects to store things. myfarm is an object you made from that class. you are "refering" to it to output what is stored inside.
27th Jun 2017, 4:50 PM
Manual
Manual - avatar