I think I get it now. myFunction(str) can either be passing by value or reference depending on the called function's parameter type. In either instance, the calls look identical. Does this sound right or am I still completely lost?
myFunction(str) can either be passing by value or reference depending on the called function's parameter type. In either instance, the calls look identical.
Yes, and this is why some people (myself included) prefer to use good ol' pointers. Because it's possible to tell what the function is taking just by looking at the call.
Yes, and this is why some people (myself included) prefer to use good ol' pointers. Because it's possible to tell what the function is taking just by looking at the call.
I've never heard that line of reasoning before, helios. That's a rather bizarre reason to avoid pass by reference.
A) Why should it matter to the caller how the callee needs to take its arguments (whether by value or by reference)?
B) In modern IDEs, any method signature is a click (or less) away so you should know exactly how it is taken.
Now, I have met coders who disliked pass by reference because they frequently forgot to add the & and ended up passing by value by mistake. But that doesn't seem to be why you are arguing against it.
Why should it matter to the caller how the callee needs to take its arguments (whether by value or by reference)?
It doesn't. It matters to the human reading the code.
In modern IDEs, any method signature is a click (or less) away so you should know exactly how it is taken.
That is, assuming you're using an IDE at that moment. I myself frequently use a regular editor like Notepad++ if I need to take a quick look at a piece of code. If you're looking at stranger code you're more likely not to use an IDE because you don't have the environment set up.
Even if it doesn't seem so, when you need to tell whether a parameter is being passed as input or output, there's a world of difference between knowing by staring at the call and knowing by hovering the mouse over it. It sounds unimportant, but trust me, it isn't.
Of course, this doesn't mean references have no use. I've found myself passing by reference when I was previously passing by value, needed to change to reference, and the amount of conflicts it would have caused was just not worth it.
A reference is a reference to another variable. In reality, the reference is the same thing as a pointer except it's already dereferenced. The above should be the same as:
1 2 3 4
void AddOne(int * myInt)
{
*myInt =+ 1;
}
Why should it matter to the caller how the callee needs to take its arguments (whether by value or by reference)?
It doesn't. It matters to the human reading the code.
Isn't passing by reference a bit faster when passing objects and strings?
// function1
int sum(int c, int d){ // passing using value
// here any changes made to c,d wont affect the original parameters
printf("1\n");
return c+d;
}
// function2
int sum(int &c,int &d){ // passing using reference
// here any changes made to c,d will affect the original parameters
printf("2\n");
return c + d;
}
// function3
int sum(int *c, int *d){ // passing using pointer
// here any changes made to c,d will affect the original parameters
printf("3\n");
return *c + *d;
}
int a,b;
sum(a,b); // we don't know if we are passing by value or by reference till we see the actual sum function
// it will call function1 or function2 whichever you have defined in the program
// defining both will surely give you error saying overloaded function
int *ptra, *ptrb;
sum(&a,&b); // it is clear that parameters are passed using pointers
// here we surely know that function3 is called
sum(ptra,ptrb); // is same as calling above method
i prefer passing parameters using pointers as it is clear and easy to understand...
Even if it doesn't seem so, when you need to tell whether a parameter is being passed as input or output, there's a world of difference between knowing by staring at the call and knowing by hovering the mouse over it. It sounds unimportant, but trust me, it isn't.
But you cannot tell that when passing pointers or references. The only thing that matters to the developer reading the code are what observable side effects a function or method call has. And that can only be determined by the const-ness of the parameters and the method.
You cannot tell the different between either of these cases by looking at the method call:
1 2
int Foo::bar(const string& str) const;
int Foo::bar(string& str);
and
1 2
int Foo::bar(const string* str) const;
int Foo::bar(string* str);
The first one in each case has no observable side effects and the second one may have observable side effects.
The behavior of the const and non-const versions are practically the same using references and pointers.