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

Discussion in 'C++' started by bo, Sep 16, 2004.

  1. bo

    bo Guest

    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
    bo, Sep 16, 2004
    #1
    1. Advertising

  2. bo

    Sharad Kala Guest

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

    "bo" <> wrote in message
    > 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...


    http://www.parashift.com/c -faq-lite/references.html#faq-8.6

    Sharad
    Sharad Kala, Sep 16, 2004
    #2
    1. Advertising

  3. Re: C++: can someone explain to me, a C/Hw guy, what the diff isbetween a reference (&) and a pointer?

    bo wrote:
    > 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.



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Sep 16, 2004
    #3
  4. Ioannis Vranos, Sep 16, 2004
    #4
  5. bo

    Sharad Kala Guest

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

    "Ioannis Vranos" <> wrote in message > Sharad Kala
    wrote:
    >
    > > http://www.parashift.com/c -faq-lite/references.html#faq-8.6

    >
    >
    >
    > 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
    Sharad Kala, Sep 16, 2004
    #5
  6. bo

    JKop Guest

    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
    JKop, Sep 16, 2004
    #6
  7. Re: C++: can someone explain to me, a C/Hw guy, what the diff isbetween a reference (&) and a pointer?

    bo wrote:
    > 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.
    Dave Rahardja, Sep 16, 2004
    #7
  8. bo

    Andre Heinen Guest

    On Thu, 16 Sep 2004 07:32:44 -0500, bo <> wrote:

    >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.

    --
    Andre Heinen
    My address, rot13-encoded: n qbg urvara ng rhebcrnayvax qbg pbz
    Andre Heinen, Sep 16, 2004
    #8
  9. bo

    steve Guest

    Re: 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" <> wrote in message
    news:...
    > 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
    >
    steve, Sep 16, 2004
    #9
  10. Re: can someone explain to me, a C/Hw guy, what the diff is between a reference (&) and a pointer?

    * Ioannis Vranos:
    > Sharad Kala wrote:
    >
    > > http://www.parashift.com/c -faq-lite/references.html#faq-8.6

    >
    > 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.

    --
    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?
    Alf P. Steinbach, Sep 16, 2004
    #10
  11. Re: can someone explain to me, a C/Hw guy, what the diff is between a reference (&) and a pointer?

    "bo" <> skrev i en meddelelse
    news:...
    > 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
    Peter Koch Larsen, Sep 16, 2004
    #11
  12. Re: C++: can someone explain to me, a C/Hw guy, what the diff is betweena 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:

    > 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
    David Lindauer, Sep 16, 2004
    #12
  13. bo

    bo Guest

    Re: 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
    bo, Sep 17, 2004
    #13
  14. bo

    Rich Grise Guest

    On Thursday 16 September 2004 06:12 am, JKop did deign to grace us with the
    following:

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

    ....
    > 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
    Rich Grise, Sep 18, 2004
    #14
  15. In message <btT2d.1520$Co1.271@trnddc02>, Rich Grise <>
    writes
    >On Thursday 16 September 2004 06:12 am, JKop did deign to grace us with the
    >following:
    >

    [...]

    >> 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.) :)
    >
    >> 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.


    See above.

    >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. :)


    --
    Richard Herring
    Richard Herring, Sep 21, 2004
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Santa
    Replies:
    1
    Views:
    1,072
    Mark A. Odell
    Jul 17, 2003
  2. man
    Replies:
    18
    Views:
    497
    Kaz Kylheku
    Nov 20, 2005
  3. Replies:
    2
    Views:
    473
  4. Macon Joe Job Guy Joe Macon Job Guy

    Att: Macon Joe Job Guy Joe Macon Job Guy Macon

    Macon Joe Job Guy Joe Macon Job Guy, Oct 14, 2007, in forum: Java
    Replies:
    0
    Views:
    369
    Macon Joe Job Guy Joe Macon Job Guy
    Oct 14, 2007
  5. Replies:
    1
    Views:
    207
    Rectal Mania
    Aug 6, 2007
Loading...

Share This Page