pointers VS reference variables

C

cppaddict

I understand that there are a couple differences between reference
variables and pointer:

1) reference variables *must* be initialized.

2) You cannot change what a reference variable refers to.

My question is: In what situations is it better to use a reference
variable over a pointer? Also, are there other differences besides
the ones I listed above?

Thanks,
cpp
 
J

Jon Bell

I understand that there are a couple differences between reference
variables and pointer:

1) reference variables *must* be initialized.

2) You cannot change what a reference variable refers to.

My question is: In what situations is it better to use a reference
variable over a pointer?

Define "better". Does it mean "simpler syntax"? Then references are
better (fewer pesky &'s and *'s to worry about). Does it mean "having
things out in the open so you can see explicitly what's going on?" Then
pointers are better, because the &'s and *'s give you clues as to what's
likely to happen to the data.

Except for those situations that *require* one or the other, using
pointers versus references is basically a religious-type issue. I
personally prefer to use references unless I have to use pointers (e.g.
when I need to be able to change which object it refers to). Some people
have the opposite preference.
 
R

Russell Hanneken

cppaddict said:
I understand that there are a couple differences between reference
variables and pointer:

1) reference variables *must* be initialized.

2) You cannot change what a reference variable refers to.

My question is: In what situations is it better to use a reference
variable over a pointer?

This is addressed in the FAQ:

http://www.parashift.com/c++-faq-lite/references.html#faq-8.6
Also, are there other differences besides
the ones I listed above?

Well, to get at the data to which a pointer points, you have to
dereference it (with operators like *, ->, or []). You don't have to do
anything like that with references.

Also, with pointers you can do pointer arithmetic. Not so with references.

And pointers allow multiple levels of indirection--pointers to pointers,
pointers to pointers to pointers, etc. That's not the case with
references; you can't declare something as a "reference to a reference"
(or even "pointer to a reference), and if you initialize a reference
with another reference, you really just end up with a reference to the
original object. As I recall, the C++ standard says references are
"names of objects," not objects themselves (unlike pointers).
 
C

c++novice

cppaddict said:
I understand that there are a couple differences between reference
variables and pointer:

1) reference variables *must* be initialized.

2) You cannot change what a reference variable refers to.

My question is: In what situations is it better to use a reference
variable over a pointer?

In very simple words
--when u know u have an object to refer to and also u'll never want to
refer to anything else other then that object use reference else use
pointer.
--use reference Also if you are using constants.
--certain operator implementation requires you to use references.
 
D

DaKoadMunky

Pointers may be null.

References may not be null.

Say you are writing a function and you know that you do not wish an argument to
be an object of type T.

That leaves you with two choices. The argument can be a pointer to T or a
reference to T.

If it is acceptable for the caller of the function to pass nothing, either
because he has nothing or because nothing can be used to indicate special
processing,
then use a pointer because it can be initialized with a null value.

If it is not acceptable for the caller of the function to pass nothing, then
use a reference because it cannot be initialized with a null value.

Enforce contracts at compile-time rather than at run-time wherever possible.

I have been looking at another object-oriented programming language as of late
and one of the things I don't like about it is that it only allows access to
objects through its version of references in a world where a reference can
always refer to nothing. Thus much of the code I have seen is littered with
statements such as (if ref!=null) or the code just throws exceptions at
run-time if a null reference is used.

IMHO the C++ methods for accessing objects indirectly are more diverse than
this other language and the slight difference between pointers and references
in C++ allow the programmer to communicate intent to fellow programmers with
the advantage of support of the compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,903
Members
47,446
Latest member
Pycoder

Latest Threads

Top