reference as parameter

When a function has, for a parameter, a reference as in

myFunction(const string& str)

does the caller normally pass a pointer i.e.

const string* strptr;
myFunction(strptr);

?

Any less clunky way to do it?
No. The caller's syntax is the same it would be if the function took an object:
1
2
std::string str;
myFunction(str);
So, just to be clear, that's a no for a different way to do it, and an implicit yes for my example?
No, the caller does not pass a pointer.

Yes, the proper way is not clunky. What you initially posted is clunky and wrong.

So would the proper way be:

const string& str;
myFunction(str);

?
you can't have a reference without referring to something.

see helios example for the proper way.
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.
Last edited on
closed account (S6k9GNh0)
1
2
3
4
void AddOne(int& myInt)
{
   myInt =+ 1;
}


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?
Last edited on
you can pass a value to a function using pointer or using reference

eg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 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...



Last edited on
Isn't passing by reference a bit faster when passing objects and strings?
I meant when comparing references to pointers.
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.
Topic archived. No new replies allowed.