Archive for March, 2010

C++ Reference Operator

March 19, 2010 Leave a comment

Reference is a C++ feature, it is not available in the C programming language. A reference is less powerful but safer than the pointer. Reference is having two names for the same variable. Let us start by looking at some examples.

int A = 5;
int& rA = A;

cout << rA << endl;

A = 7;

cout << rA << endl;



No matter which variable’s value gets changed both A and rA change.

References can be used to replace pointers in some cases. As you may recall functions in c/c++ can only return one value and programmers use pointers in order to get around that! References can be used instead of pointers and you do not have to use the * operator in your functions.

Categories: OOP344