Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Why do you need references when you have pointers? [duplicate]

Possible Duplicate:
C++: When to use References vs. Pointers
Could operator overloading have worked without references?

I couldn't help it, but this fundamental question was on my mind: why does C++ have references when you could do with pointers just as well?

I know that in certain situations they are slightly safer and more often than not they make the code prettier, but technically there is no difference, right? So are there any situations where I couldn't do with a pointer and a reference is a must?
I would like to see specific examples of when using references is unavoidable.

Disclaimer:
I haven't found any answers to this on StackOverflow, this is not a question about the differences in syntax. I am wondering why the C++ language introduced references in the first place.

like image 985
Eitan T Avatar asked May 28 '12 08:05

Eitan T


People also ask

Why do we need references when we have pointers?

Pointers: A pointer is a variable that holds memory address of another variable. A pointer needs to be de referenced with * operator to access the memory location it points to.

Why are references better than pointers?

References are used to refer an existing variable in another name whereas pointers are used to store address of variable. References cannot have a null value assigned but pointer can. A reference variable can be referenced by pass by value whereas a pointer can be referenced but pass by reference.

What is an advantage of using a reference over a raw pointer?

The main advantages of references are that they always contain a valid value, are cleaner than pointers, and support polymorphism without needing any extra syntax. If none of these advantages apply, there is no reason to prefer a reference over a pointer.

Are references more efficient than pointers?

It's much faster and memory-efficient to copy a reference than to copy many of the things a reference is likely to refer to. Pointers almost always point to data allocated on the heap. Pointers can be (and frequently are) null.


3 Answers

  • Using pointers alone you can't properly pass-by-reference or return-by-reference.
  • Some methods require a reference. How would you implement a copy-constructor?
  • Sometimes you need to enforce aliasing. You can't do that with pointers - they can be changed. References cannot bind to something different. So when you initialize a reference, you guarantee it will refer to the same object through its scope.
  • The safety issue
  • (const)References can bind to temporary objects. To create a temporary pointer, you'd need to free it inside the method you pass it to. But you can't tell if it's a temporary or not inside.
like image 71
Luchian Grigore Avatar answered Oct 18 '22 22:10

Luchian Grigore


Operator overloading. Using pointers for "passing via reference" would give you unacceptable syntax.

like image 8
zch Avatar answered Oct 19 '22 00:10

zch


If you have a pointer as a parameter, you have to check if it is NULL. With references, you do have to make that check. Here is why there is references in C++ from the man himself - http://www.stroustrup.com/bs_faq2.html#pointers-and-references

like image 3
Superman Avatar answered Oct 18 '22 22:10

Superman