Link to home
Start Free TrialLog in
Avatar of LeelaSwamy
LeelaSwamy

asked on

Why use reference instead of pointers?

Whatz the advantage of reference varaibles over pointers?
ASKER CERTIFIED SOLUTION
Avatar of chofreither
chofreither

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlexFM
AlexFM

In some situations reference is an only way. For example, if you write:

cout << n << endl;

operator<< must return reference to the output stream, otherwise such string cannot be compiled. References are used in situations like this to create a temporary internal objects. You can find such samples, for example, in STL.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"references" are just slighty gussied up pointers.  There's a little more sophisticated  type-compatibility checking, so references are slightly less crash-producing than raw pointers.  You also can forget that they're actually pointers and use the less eye-jarring point.y syntax instead of point->y.

Unfortunately, since C++ still has to support pointers and pointer arithmetic, it's still all too easy to screw up references, but at least you have to work at it a bit more.

Avatar of DanRollins
>> Unfortunately, since C++ still has to support pointers and pointer arithmetic

"Unfortunately" C++ still doesn't pretend that a pointer to an object is the object itself.  

>>Whatz the advantage of reference varaibles over pointers?

There is none.  The only reason to use references is if you don't understand how to use pointers.

I hope that clears things up :)
-- Dan
It's wrong that there is no advantage. Operator overloading, for instance, can only be efficiently done with references.
You HAVE to have pointers. HOW you gonna put anything on the heap?

References are not just another way to write pointers. There are several very important differences between references and pointers.

Operators act differently on reference variables than they do on pointers.  For instance;

int i,j,k;
int &ri, &rj, &rk;
int *pi, *pj, *pk;
// initialization code here
i = j - k;
ri = rj - rk;
pi = pj - pk;// error

The - operator means subtraction to integers, and also to integer references. The - operator means the difference (of type diff_t) between two pointers in pointed-to units (ints in this case). Arithmetic operators act on the referenced type when operands are references. This is important because it lets you write natural looking code for arithmetic on big types like matrices, tensors, etc.

Matrix<4,4> &A, &B, &C;
// initialization code here
A = B + C;

References have the quick argument passing semantics of pointers, but the lexical appearance of the referenced variables.

Another important property of references is that there is no null value like there is for pointers. Without doing dirty compiler tricks there is no way to convert 0 into a reference. When you declare a variable (particularly a function formal argument) reference, you are declaring a promist that the referenced object exists.

void f1(Foo& rf)
{
rf.operation(); }

void f2(Foo* pf)
{
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
LeelaSwamy,
You have posted a total of five questions, and all five questions are open, and have not been awarded points.

You have 5 questions open in the C++ topic area, some of which are two months old.

Please award points for your previous questions and closed them.
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: chofreither {http:#9238772} & mnashadka {http:#9238796} & shajithchandran {http:#9238955} & AlexFM {http:#9239018} & guntherothk {http:#9355826}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer