What is & at the end of method? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is & at the end of method?

Hi I have no idea about importance of & at mymethod1 function. Can someone help me understand meaning of this and how it differ from normal function? https://code.sololearn.com/cQxJFGsadqQi/?ref=app

11th Mar 2023, 6:12 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
10 Answers
+ 2
it's a reference-qualifier introduced in c++11 It is a way to help the compiler decide which function to call when the object is an lvalue or an rvalue. An lvalue refers to an object that persists beyond a single expression. An rvalue is a temporary value that does not persist beyond the expression that uses it. while && is a cv-qualifier I don't know about this in details maybe you can look for official documentation of c++.
11th Mar 2023, 6:24 PM
I am offline
I am offline - avatar
+ 2
& at the end of a method means that this function can only be called on non-const lvalues. This means the object must be non-const non-temporary. You can also qualify the function with: const, const& and && "const" means the object must be const but may be lvalue or rvalue "const&" means the object must be const lvalue "&&" means the object must be non-const rvalue You can also technically have "const&&" but it has no practical use, so that's why it's almost never used.
12th Mar 2023, 9:00 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
Thanks Snehil[Less Active] Good to know this. Now, i am wondering why a different function is required for l value or r value ? If i am adding two objects, i should be adding those be it a l value or rvalue. Thanks again for sharing this. I am just more curious to know usecase.
11th Mar 2023, 7:29 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
A clear explanation but: a `const` method can be called on a non-`const` object; it simply promises not to change the object. That no-changes promise can be very useful for understanding code and proving correctness. When necessary, e.g. for caching results that are expensive to compute, one can work around the self-imposed limitation by using indirection and/or `mutable`.
12th Mar 2023, 10:21 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
In PHP, the & sign before a parameter in a function declaration is used to indicate that the function parameter is passed by reference rather than by value. When a parameter is passed by reference, any changes made to the parameter inside the function also affect the original variable outside the function. In contrast, when a parameter is passed by value, a copy of the value is passed to the function, so any changes made to the parameter inside the function do not affect the original variable outside the function.
13th Mar 2023, 12:00 PM
SANA SIPOU
SANA SIPOU - avatar
+ 1
Same thing is there in c++ as well but this query is not for & with parameter. It is for & after function close paenthesis. For example, void add(int a,int b)&
13th Mar 2023, 12:03 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Ketan Lalcheta it's not so as I remember we can overload same function also for L and R value
12th Mar 2023, 7:24 AM
I am offline
I am offline - avatar
0
Snehil[Less Active] i am not saying that we can overload for l or r value. Sorry for confusion got created. What i meant was the need of overloaded function is known to me. ASR i got theory part now. What confuses me is the example of same. When do we need some function calls based on l or r value of caller ?
12th Mar 2023, 9:44 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
I mean what difference can be achieved in both function body of l nd r value ? If not maintained , what would be issue with just going for a single function body for l nd r value caller ?
12th Mar 2023, 9:45 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
I think you are not getting my point. I got what all theory you told about const and non const as well as l value and r value. Let me re phrase as below : I am just taking this example here for overload. Add(int ,int) and add(float,float) is needed as we could need to add two int or two float. Add logic remains same for int and float . Hence we need this overloading Now, my question is for ref qualifier. Void dosomething() . What need to be done inside based on ref qualifier ?
12th Mar 2023, 1:08 PM
Ketan Lalcheta
Ketan Lalcheta - avatar