A reference serves as an alternative name for an object. In real-world programs, references are primarily used as formal parameters to functions.
A reference is a compound type that is defined by preceding a variable name by the & symbol. A compound type is a type that is defined in terms of another type. In the case of references, each reference type “refers to” some other type. We cannot define a reference to a reference type, but can make a reference to any other data type.
A reference must be initialized using an object of the same type as the reference:


#include <iostream>

int main() {
	int ival = 1024;
	int &refVal = ival; // ok: refVal refers to ival

	int &refVal2; // error: a reference must be initialized
	int &refVal3 = 10; // error: initializer must be an object

	return 0;

You may also like...