Home > OOP344 > C++ Reference Operator

C++ Reference Operator

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;

Output:

5
7

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
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: