C++: can someone explain to me, a C/Hw guy, what the diff is between a reference (&) and a pointer?

B

bo

And why and where one should use one vs. the other?

Verbally, it seems like semantics to me--but obviously there is some
actual difference that makes references different and or preferable
over pointers in some cases...

TIA
 
I

Ioannis Vranos

bo said:
And why and where one should use one vs. the other?

Verbally, it seems like semantics to me--but obviously there is some
actual difference that makes references different and or preferable
over pointers in some cases...


References are more easy to be optimised out.
 
S

Sharad Kala

Ioannis Vranos said:
I do not find that FAQ particularly useful. More accurately I believe it
is full of (nicely termed) nonsense.
Do you consider it useful?

Well, I think they could have been little more clearer (considering it's
FAQ). Basically references are preferred when one knows that one needs to
refer to the same object and that won't change. In fact it's quite possible
that compilers implement references in terms of T* const. Also references
offer the advantage that you don't need to check for null-ness so many if
loops are avoidable.

Sharad
 
J

JKop

bo posted:
And why and where one should use one vs. the other?

Verbally, it seems like semantics to me--but obviously there is some
actual difference that makes references different and or preferable
over pointers in some cases...

TIA


References are nicer to work with, in that they behave
exactly like real variables.

Pointers are... different...

As I always I prefer the more familiar. Here's what I do:

A) Use a references. If I can't... go to plan B.

B) Use a pointer.


The only advantage references have over pointers is that
they behave exactly like your familiar variables. (Some
people may also argue that you're guaranteed that they
refer to a legitimate object, I don't buy this though...)

The advantage that pointers have over references are:

a) They can be reseated. One minute they can point East,
the next South West.

b) They can be incremented/decremented. If you have a
pointer to a character, you simply increment it and it
points to the next character in the string.

So...

Use references. If you can't, eg. you need to increment it
or seseat it, then *resort* to pointers.


-JKop
 
D

Dave Rahardja

bo said:
And why and where one should use one vs. the other?

Verbally, it seems like semantics to me--but obviously there is some
actual difference that makes references different and or preferable
over pointers in some cases...

TIA

"Use references when you can, use pointers when you must" is my mantra.

Under the hood, both references and pointers are implemented using
pointers. Semantically however, references are "bound" to specific
variables, and do not "point" to arbitrary variables. That probably sums
up the difference between the two entities.

Using references makes my code cleaner and less error-prone because I
cannot manipulate a reference in such away that it "points" away from
the intended variable.

Using references also allows me to change a pass-by-value parameter to a
pass-by-reference without modifying the calling code. Consider these
function prototypes:

void Function_A(const Large_Class in);
void Function_B(const Large_Class& in);

These two functions are called in exactly the same way--Function_A(obj),
Function_B(obj). But the resulting calls are quite different. References
allow me to change the parameter-passing method as I develop my code
with minimal impact.
 
A

Andre Heinen

And why and where one should use one vs. the other?

Verbally, it seems like semantics to me--but obviously there is some
actual difference that makes references different and or preferable
over pointers in some cases...

Pointers are not suitable for operator overloading. Use
references in that case.

You cannot use references in standard containers. E.g. you must
write vector<X*> instead of vector<X&>. Use pointers in that
case.

References are guaranteed to refer to something. They cannot be
null. Use references unless you need the null value.

A reference cannot be reseated to another object. Use references
unless you need pointer arithmetic.

If a function modifies its argument, some programmers feel that
pointers are better because it makes it obvious that the arg can
be changed: f(&i)
Other programmers use a reference but choose a self-explicit
function name: modify(i)

Can't think of anything else right now...

Check
http://www.research.att.com/~bs/bs_faq2.html#pointers-and-references
And also, of course, section 8 of this newsgoup's FAQ.
 
S

steve

Added to what everyone else has said, it is also important to understand the
implication of ownership when deciding whether to use a reference or a
pointer.

Pointer are generally used to take ownership ( responsible for deleting it
if it was new'd ) of an object, whereas a reference would be use when
someone else has ownership of the object.

While you could implement the "ownership/non-ownership" using a pointer in
both cases, it does make the code self documenting, and lets the compiler
check for bugs :)

HTH
-Steve
 
A

Alf P. Steinbach

* Ioannis Vranos:
I do not find that FAQ particularly useful. More accurately I believe it
is full of (nicely termed) nonsense.

Do you consider it useful?

The FAQ explained:


<q>Use references when you can, and pointers when you have to.</q>

Explanation: if you can use a reference, use a reference.

Otherwise, use a pointer.

This is good advice because references are more limited than pointers, so to
screw up you have to work much harder at it when all you have is a reference.



<q>References are usually preferred over pointers whenever you don't need
"reseating"</q>

Explanation: if you don't need to let the same variable refer to different
objects at different times, you can probably use a reference, and so you
should probably use a reference.


<q>This usually means that references are most useful in a class's public
interface.</q>

Explanation: the public interface of a class let's you pass information into
and get information out of an object. Unless there is a need for a logical
null-value this can be done using references. Hence use references here.


<q>References typically appear on the skin of an object, and pointers on the
inside.</q>

Explanation: in the code that implements an object pointers may be needed
because there one may need to let the same variable refer to different objects
at different times, or handle logical null-values. For example, in a linked
list class you need pointers inside. But the interface can be and probably
should be expressed with references only, giving the client code existence
guarantees that greatly simplifies the client code.


<q>The exception to the above is where a function's parameter or return value
needs a "sentinel" reference.</q>

Explanation: this is the logical null-value referred to above.


<q>This is usually best done by returning/taking a pointer, and giving the
NULL pointer this special significance (references should always alias
objects, not a dereferenced NULL pointer).</q>

Explanation: here Marshall Cline is expressing an opinion, which e.g. Bertrand
Meyer (the Eiffel man) seems to be diametrically opposed on. I'm personally
somewhere in between, choosing whatever seems most practical. But I think I
mostly lean in the direction Marshall is pointing here: it's too cumbersome to
have isVoid() functions, especially since a functional interface isn't
something that usually can be passed further around as data, as a pointer can.


<q>Note: Old line C programmers sometimes don't like references since they
provide reference semantics that isn't explicit in the caller's code.</q>

Explanation: people often do things out of habit and then rationalize it by
inventing reasons. Flame wars have therefore been fought over whether an "&"
in the client code indicates -- or not -- that an actual argument is an
out or an in/out (as opposed to pure in) argument. The facts are (1) that "&"
in the client code tells you nothing, (2) that in the C language the lack of
"&" tells you that argument is pure in, and (3) that in C++ the lack of "&"
doesn't tell you anything, either: you need to know about the function called.


<q>After some C++ experience, however, one quickly realizes this is a form of
information hiding, which is an asset rather than a liability.</q>

Explanation: this is not a technical comment but a methodological one. I
happen to agree with Marshall here but it's not universally agreed on. Anyway
the only connection to C++ is that the "&"-issue is a concrete example.


<q>E.g., programmers should write code in the language of the problem rather
than the language of the machine.</q>

Explanation: same as the previous one.
 
P

Peter Koch Larsen

bo said:
And why and where one should use one vs. the other?

Verbally, it seems like semantics to me--but obviously there is some
actual difference that makes references different and or preferable
over pointers in some cases...

TIA

Have you read the faq pointed to by others? I believe it is quite good. No
specific implementation is required in the standard. That said, you can be
quite sure that a reference in the general case will be represented by a
pointervalue. In special cases, the value of the reference will be known and
no pointer will be stored.

/Peter
 
D

David Lindauer

the main usefulness in my mind is maintainability; once you create a
reference variable you *know* it is always going to point to the same
thing, and that it is going to be non-null. But if you are reading code
with pointers in it you may not be sure that the author isn't going to
go changing the meaning later down in the code, and if he does you may
not be able to tell immediately whether it was intended or a bug.
Internally however, compilers I have seen have typically implemented
reference variables the same way they did pointers in terms of low-level
behavior.

Personally I spend a *lot* more time maintaining and reusing than I do
cutting new code, so any help I can get in terms of cues as to the
intended purpose and/or limitations of things are really helpful.

David
 
B

bo

Thanks to all who replied and the pointer (reference?) to the FAQ on
the subject! I am enlightened.....

Regards,

Bo
 
R

Rich Grise

bo posted:
....
References are nicer to work with, in that they behave
exactly like real variables.
a) They can be reseated. One minute they can point East,
the next South West.

This is probably just style, but this is why Pointers Are
Bad(TM). ;-)
b) They can be incremented/decremented. If you have a
pointer to a character, you simply increment it and it
points to the next character in the string.

I wouldn't even do this. If I really had to reference
string chars, I'd probably use *(ptr + i) and so on (i.e.,
not change the pointer itself if I didn't have to.) but
I guess we've already covered that, haven't we? (i.e. then
use a reference.) :)
So...

Use references. If you can't, eg. you need to increment it
or seseat it, then *resort* to pointers.

Yeah - I cut my teeth on C with K&R at my elbow, and a tutor
who was about as smart as me - I wrote in a lot of printfs. ;-)
and have done a little thinking about pointers vs. references,
and I can't think of a single thing I'd want to do in real C++
that would need a pointer at all. I'm not porting any legacy
code - I intend to lift all new snippets to paste. ;-)
(I can pass for a programmer, but mostly what I do is copy
and paste other people's code. :)

Cheers!
Rich
 
R

Richard Herring

Rich Grise <[email protected]> said:
[...]
b) They can be incremented/decremented. If you have a
pointer to a character, you simply increment it and it
points to the next character in the string.

I wouldn't even do this. If I really had to reference
string chars, I'd probably use *(ptr + i) and so on (i.e.,
not change the pointer itself if I didn't have to.)

Pointers Are Iterators, so there's no harm in using the natural iterator
operations with them.
but
I guess we've already covered that, haven't we? (i.e. then
use a reference.) :)


Yeah - I cut my teeth on C with K&R at my elbow, and a tutor
who was about as smart as me - I wrote in a lot of printfs. ;-)
and have done a little thinking about pointers vs. references,
and I can't think of a single thing I'd want to do in real C++
that would need a pointer at all.

See above.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top