Connecting Tech Pros Worldwide Forums | Help | Site Map

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

bo
Guest
 
Posts: n/a
#1: Jul 22 '05
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


Sharad Kala
Guest
 
Posts: n/a
#2: Jul 22 '05

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



"bo" <bo@cephus.com> wrote in message[color=blue]
> 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...[/color]

http://www.parashift.com/c++-faq-lit...s.html#faq-8.6

Sharad


Ioannis Vranos
Guest
 
Posts: n/a
#3: Jul 22 '05

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


bo wrote:[color=blue]
> 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...[/color]


References are more easy to be optimised out.



--
Ioannis Vranos

http://www23.brinkster.com/noicys
Ioannis Vranos
Guest
 
Posts: n/a
#4: Jul 22 '05

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


Sharad Kala wrote:
[color=blue]
> http://www.parashift.com/c++-faq-lit...s.html#faq-8.6[/color]



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

Do you consider it useful?



--
Ioannis Vranos

http://www23.brinkster.com/noicys
Sharad Kala
Guest
 
Posts: n/a
#5: Jul 22 '05

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



"Ioannis Vranos" <ivr@guesswh.at.grad.com> wrote in message > Sharad Kala
wrote:[color=blue]
>[color=green]
> > http://www.parashift.com/c++-faq-lit...s.html#faq-8.6[/color]
>
>
>
> I do not find that FAQ particularly useful. More accurately I believe it
> is full of (nicely termed) nonsense.
> Do you consider it useful?[/color]

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




JKop
Guest
 
Posts: n/a
#6: Jul 22 '05

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


bo posted:
[color=blue]
> And why and where one should use one vs. the other?
>
> Verbally, it seems like semantics to me--but obviously[/color]
there is some[color=blue]
> actual difference that makes references different and or[/color]
preferable[color=blue]
> over pointers in some cases...
>
> TIA[/color]


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
Dave Rahardja
Guest
 
Posts: n/a
#7: Jul 22 '05

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


bo wrote:[color=blue]
> 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
>[/color]

"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.
Andre Heinen
Guest
 
Posts: n/a
#8: Jul 22 '05

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


On Thu, 16 Sep 2004 07:32:44 -0500, bo <bo@cephus.com> wrote:
[color=blue]
>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...[/color]

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_f...and-references
And also, of course, section 8 of this newsgoup's FAQ.

--
Andre Heinen
My address, rot13-encoded: n qbg urvara ng rhebcrnayvax qbg pbz
steve
Guest
 
Posts: n/a
#9: Jul 22 '05

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


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

"bo" <bo@cephus.com> wrote in message
news:9q1jk01jqfp8rnri88udpdcmfqvp8clpgu@4ax.com...[color=blue]
> 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
>[/color]


Alf P. Steinbach
Guest
 
Posts: n/a
#10: Jul 22 '05

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


* Ioannis Vranos:[color=blue]
> Sharad Kala wrote:
>[color=green]
> > http://www.parashift.com/c++-faq-lit...s.html#faq-8.6[/color]
>
> I do not find that FAQ particularly useful. More accurately I believe it
> is full of (nicely termed) nonsense.
>
> Do you consider it useful?[/color]

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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Peter Koch Larsen
Guest
 
Posts: n/a
#11: Jul 22 '05

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



"bo" <bo@cephus.com> skrev i en meddelelse
news:9q1jk01jqfp8rnri88udpdcmfqvp8clpgu@4ax.com...[color=blue]
> 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
>[/color]

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


David Lindauer
Guest
 
Posts: n/a
#12: Jul 22 '05

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


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

bo wrote:
[color=blue]
> 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[/color]
bo
Guest
 
Posts: n/a
#13: Jul 22 '05

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


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

Regards,

Bo

Rich Grise
Guest
 
Posts: n/a
#14: Jul 22 '05

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


On Thursday 16 September 2004 06:12 am, JKop did deign to grace us with the
following:
[color=blue]
> bo posted:
>[color=green]
>> And why and where one should use one vs. the other?
>>[/color][/color]
....[color=blue]
> References are nicer to work with, in that they behave
> exactly like real variables.[/color]
[color=blue]
> a) They can be reseated. One minute they can point East,
> the next South West.[/color]

This is probably just style, but this is why Pointers Are
Bad(TM). ;-)
[color=blue]
> 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.[/color]

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.) :-)
[color=blue]
> So...
>
> Use references. If you can't, eg. you need to increment it
> or seseat it, then *resort* to pointers.[/color]

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

Richard Herring
Guest
 
Posts: n/a
#15: Jul 22 '05

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


In message <btT2d.1520$Co1.271@trnddc02>, Rich Grise <null@example.net>
writes[color=blue]
>On Thursday 16 September 2004 06:12 am, JKop did deign to grace us with the
>following:
>[/color]
[...]
[color=blue][color=green]
>> 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.[/color]
>
>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.)[/color]

Pointers Are Iterators, so there's no harm in using the natural iterator
operations with them.
[color=blue]
>but
>I guess we've already covered that, haven't we? (i.e. then
>use a reference.) :-)
>[color=green]
>> So...
>>
>> Use references. If you can't, eg. you need to increment it
>> or seseat it, then *resort* to pointers.[/color]
>
>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.[/color]

See above.
[color=blue]
>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. :-)[/color]

--
Richard Herring
Closed Thread