469,898 Members | 1,779 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

pointers VS reference variables

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
Jul 22 '05 #1
4 4782
In article <kj********************************@4ax.com>,
cppaddict <he***@hello.com> wrote:
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.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #2
cppaddict wrote:
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-lit...s.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).

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Jul 22 '05 #3
cppaddict <he***@hello.com> wrote in message news:<kj********************************@4ax.com>. ..
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.
Also, are there other differences besides
the ones I listed above?

Thanks,
cpp

Jul 22 '05 #4
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.


Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Neil Zanella | last post: by
2 posts views Thread by Dmitri Islentyev | last post: by
16 posts views Thread by jryden | last post: by
3 posts views Thread by v4vijayakumar | last post: by
3 posts views Thread by Rahul | last post: by
9 posts views Thread by sulekhasweety | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.