What are references useful for?

What a references useful for?

Ex
1
2
 int ival = 0;
    Int &rval = ival
Last edited on
Usually they are useful for passing objects to functions by reference.
I am a beginner myself so if any pros think that I am wrong, please tell me.

You usually pass something by reference, if it's a large struct or an object.
The reason for this is that passing literally copies of big chunks of data, is inefficient.
When you pass something by reference, you simply pass an address instead, that refers to/points at the struct or object.

Compare this with real life. When you for example need to repair your house, you do not send your entire house, you only send a reference or an address that points to where that house is so that they can come and repair it instead.
Compare this with real life. When you for example need to repair your house, you do not send your entire house, you only send a reference or an address that points to where that house is so that they can come and repair it instead.


That sounds like pointers. A reference is more like a voodoo doll that you manipulate to affect the represented person.
First and for emost, references are useful when you want to be able to modify the attributes of an object (we're saying an object, because it is a term with more general scope that applies both to built-in and user-defined types). Second, as the others have pointed out, the overhead of passing large objects can be reduced by passing them by reference. If you don't want to modify an object in a method, yet you need to use reference, use the const keyword along with the reference. Third, using references can save you some formal tests (for example, you don't have to check for a nullptr pointer). And lastly, it does make for a cleaner code overall. For example, compare the implementations of the Singleton pattern with pointers and with references.
This question can't be answered with a straightforward response. There are places where you will need to use references and there are places where pointers can do the same thing. But I find that references look cleaner overall than pointers when it comes to manipulating data in functions.

For example consider the swap function, one that uses references and one that uses pointers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdio>
template <typename T> void swap( T &a, T &b ) {
	puts("Reference swap called");
	T temp(a);
	a = b;
	b = temp;
}

template <typename T> void swap( T *a, T *b ) {
	puts("Pointer swap called");
	T temp(*a);
	*a = *b;
	*b = temp;
}

int main(void) {
	char c('f'), d('g');
	printf("c = %c\t|   d = %c\n", c, d);
	swap(c, d);
	printf("c = %c\t|   d = %c\n", c, d);
	swap(&c, &d);
	printf("c = %c\t|   d = %c\n", c, d);
	return 0;
}


Although both functions do the same thing, the reference looks better and easily understandable than pointers. So it is all a matter of preference.

For more information:
http://www.dgp.toronto.edu/~patrick/csc418/wi2004/notes/PointersVsRef.pdf
Last edited on
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

A good guide for why passing by reference can be good :).
Topic archived. No new replies allowed.