C++ Reference

Reference variable is an alias, another name for an existing variable (memory location).

Declaring References

To declare a reference of type int, use int&.

#include <iostream>
int main() {
	int a = 5;
	int &b = a; // b is another name for a
	int* c = &b; // c is a pointer pointing to a		
}
&a = 0x7fffd5ef7c44
&b = 0x7fffd5ef7c44
&c = 0x7fffd5ef7c48
c = 0x7fffd5ef7c44

Passing by References

Function parameter with reference type are passed by reference like passing by pointer but without the extra syntax inside the function

// if you have int a = 1, b = 2, then call like this:
// swap (a,b) (no ampersands)!
void swap (int& a, int &b) {
	int tmp = a;
	a = b;
	b = tmp;
}

C++ has both pass by value (non-reference parameters) and pass by reference (reference parameters).
Functions can have a mix of both pass-by-values and pass-by-references.

When passing objects in C++, we generally pass by reference because no excessive copying occurs, and this decreases runtime.

Returning References

References can be returned:

int& minref(int& a, int& b) {
	if (a<b) {
		return a;
	} else {
		return b
	}
}

int main() {
	int a = 5, b = 10;
	int& min = minref(a,b);
	minref(a, b);
	min = 12;
}

Const Reference

A reference can be a const. You can't subsequently assign via that reference, but you can still assign the original non-const variable, or via a non-const reference to it.