6
Let’s assume I have two functions:
int soma_a(int a, int b){
return a + b;
}
and
int soma_b(const int& a, const int& b){
return a + b;
}
What would be the difference between soma_a
and soma_b
6
Let’s assume I have two functions:
int soma_a(int a, int b){
return a + b;
}
and
int soma_b(const int& a, const int& b){
return a + b;
}
What would be the difference between soma_a
and soma_b
5
The first is being passed by value, that is, the value of the argument being used in the call of this function is copied to the function parameter soma_a()
.
The second argument value is not copied, a reference is used in place, i.e., an opaque pointer is created by pointing to the location where the argument value is and that address is copied to the function parameter soma_b()
. How there’s a const
means that the function is prohibited from changing this value, it will be read only, so there is a contract guaranteeing to the caller that the value will be intact.
In a int
doesn’t make sense because the int
It’s usually the same size or even smaller than the size of a pointer, so copying costs the same or more. usually this is more useful in cases where the past structure is larger than a pointer.
In written form both will behave exactly the same, but the former will be more efficient in this specific case.
If I didn’t have the const
then it could be useful to make the parameter serve as output as well, then changing its value within the function would cause a side effect on the argument, after all the argument and parameter are in the same address, you passed the address, not a value independently, they are completely linked because it is the same thing. In this case the argument has to be a variable.
#include <iostream>
using namespace std;
int soma_b(int& a, const int& b) {
a = 4;
return a + b;
}
int main() {
int x = 2;
cout << soma_b(x, 3) << endl;
cout << x;
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
important readings to better understand the subject and some related ones, even if in another language:
Change something in the performance?
Other than what I said, no.
"As there is a const means that the function is prohibited from changing this value" - except if you use const_cast
and the original object does not have the qualifier const
, in this case you can modify the referenced.
@Márioferoldi yes, it is possible to circumvent, but you are explicitly saying a f0d@up for the established contract
0
In C++ , in addition to variables, we can also use numbers or characters whose values do not change. They are called constants. In this case, in soma_b
, you make reference to the variables a
and b
as constants, therefore, their values cannot be changed. (not even with a command cin >> a >> b >> endl;
Browser other questions tagged c++ parameters reference const
You are not signed in. Login or sign up in order to post.
Thread similar
– Tuxpilgrim
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.
– Maniero