reference vs pointer .. ?

Discussion in 'C++' started by m sergei, Jul 1, 2004.

  1. m sergei

    m sergei Guest

    Question is about the difference between these two

    reference to an object versus pointer to an object

    What are advantages in either one and when would one use them. I see
    lots of examples when an object is passed as a parameter to a function
    it is passed as a reference. Though it would have been fine to pass it
    as a pointer as well ? is that right ?
    I had posted a similar question in the C lang newsgroup but did not
    get much help as C does not do references to objects.
     
    m sergei, Jul 1, 2004
    #1
    1. Advertising

  2. On 30 Jun 2004 21:06:34 -0700, m sergei <> wrote:

    > Question is about the difference between these two
    >
    > reference to an object versus pointer to an object
    >
    > What are advantages in either one and when would one use them. I see
    > lots of examples when an object is passed as a parameter to a function
    > it is passed as a reference. Though it would have been fine to pass it
    > as a pointer as well ? is that right ?


    It would work if passed as a pointer. But

    1) A pointer can be NULL, so the function must test for that.

    2) A pointer cannot be initialised with a temporary, so you lose
    flexibility

    X f();
    void g(const X&);
    g(f()); // fine

    X f();
    void g(const X*);
    g(&f()); // compile error


    > I had posted a similar question in the C lang newsgroup but did not
    > get much help as C does not do references to objects.


    Not surprising really.

    john
     
    John Harrison, Jul 1, 2004
    #2
    1. Advertising

  3. > 1) A pointer can be NULL, so the function must test for that.
    >
    > 2) A pointer cannot be initialised with a temporary, so you lose
    > flexibility


    and

    3) A pointer can be indexed while a reference can not. So by using a
    reference you implicit state that this is not an array.

    4) The syntax for using a reference is the same as for using an object on
    the stack, so it is easier to rewrite code that previously worked on a stack
    object to use a reference than to use a pointer.

    Niels Dybdahl
     
    Niels Dybdahl, Jul 1, 2004
    #3
  4. On Thu, 1 Jul 2004 10:23:06 +0200, "Niels Dybdahl"
    <-graphics.com> wrote:

    >and
    >
    >3) A pointer can be indexed while a reference can not. So by using a
    >reference you implicit state that this is not an array.


    Not necessarily.

    For references to built-in types, this is true. However, user-defined
    types can overload operator[], and you can of course index a reference
    to a pointer of any type. The mere fact that you have a reference
    doesn't automatically prohibit the use of operator[] from the
    standpoint of the grammar.


    --
    Bob Hairgrove
     
    Bob Hairgrove, Jul 1, 2004
    #4
  5. m sergei

    Unforgiven Guest

    "Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
    news:...
    > On Thu, 1 Jul 2004 10:23:06 +0200, "Niels Dybdahl"
    > <-graphics.com> wrote:
    >
    >>and
    >>
    >>3) A pointer can be indexed while a reference can not. So by using a
    >>reference you implicit state that this is not an array.

    >
    > Not necessarily.
    >
    > For references to built-in types, this is true. However, user-defined
    > types can overload operator[], and you can of course index a reference
    > to a pointer of any type. The mere fact that you have a reference
    > doesn't automatically prohibit the use of operator[] from the
    > standpoint of the grammar.


    No, but if you have a type that defines operator[], using [] on a pointer to
    it would index the pointer, while using it on a reference indexes the
    original type. Using a pointer in such a scenario can lead to subtle bugs,
    and at the very least ugly syntax such as (*somevar)[2].

    --
    Unforgiven
     
    Unforgiven, Jul 1, 2004
    #5
  6. On Thu, 1 Jul 2004 11:35:28 +0200, "Unforgiven"
    <> wrote:

    >"Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
    >news:...
    >> On Thu, 1 Jul 2004 10:23:06 +0200, "Niels Dybdahl"
    >> <-graphics.com> wrote:
    >>
    >>>and
    >>>
    >>>3) A pointer can be indexed while a reference can not. So by using a
    >>>reference you implicit state that this is not an array.

    >>
    >> Not necessarily.
    >>
    >> For references to built-in types, this is true. However, user-defined
    >> types can overload operator[], and you can of course index a reference
    >> to a pointer of any type. The mere fact that you have a reference
    >> doesn't automatically prohibit the use of operator[] from the
    >> standpoint of the grammar.

    >
    >No, but if you have a type that defines operator[], using [] on a pointer to
    >it would index the pointer, while using it on a reference indexes the
    >original type. Using a pointer in such a scenario can lead to subtle bugs,
    >and at the very least ugly syntax such as (*somevar)[2].


    That's true, but that certainly wasn't what I was referring to. The
    issue was whether one could index a reference or not. I wasn't talking
    about indexing into a pointer, except in the context of reference to
    pointer.

    Yet another reason to use STL container classes and avoid arrays and
    pointers if possible.

    --
    Bob Hairgrove
     
    Bob Hairgrove, Jul 1, 2004
    #6
  7. m sergei

    m sergei Guest

    void g(const X&) why is const required ? is that a requirement to inform the
    compiler ?


    also your second function void g(const X*).
    is void g(const *X) same as above ? (or correct ?)

    > It would work if passed as a pointer. But
    >
    > 1) A pointer can be NULL, so the function must test for that.
    >
    > 2) A pointer cannot be initialised with a temporary, so you lose
    > flexibility
    >
    > X f();
    > void g(const X&);
    > g(f()); // fine
    >
    > X f();
    > void g(const X*);
    > g(&f()); // compile error
     
    m sergei, Jul 1, 2004
    #7
  8. "m sergei" <> wrote in message
    news:...
    > void g(const X&) why is const required ? is that a requirement to inform

    the
    > compiler ?
    >
    >
    > also your second function void g(const X*).
    > is void g(const *X) same as above ? (or correct ?)
    >


    Only const references can be bound to temporaries

    X f();
    void g(const X&);
    void h(X&);

    g(f()); // legal
    h(f()); // illegal

    Not all compilers enforce this rule however.

    The pointer case is illegal with or without const however.

    john
     
    John Harrison, Jul 1, 2004
    #8
  9. m sergei

    Rolf Magnus Guest

    m sergei wrote:

    > void g(const X&) why is const required ? is that a requirement to
    > inform the compiler ?


    It tells the compiler (as well as the programmer) that your function
    doesn't modify the object it gets passed. Without it, you can't pass
    constant objects to the function, and you can't pass temporary objects.

    > also your second function void g(const X*).
    > is void g(const *X) same as above ? (or correct ?)


    No, it's not correct. * has to follow the name of a type to show that
    you want a pointer to that type. 'const' is not a type.
     
    Rolf Magnus, Jul 1, 2004
    #9
  10. m sergei

    JKop Guest

    int& k = ... ;

    (&k)[5] = 34;


    -JKop
     
    JKop, Jul 1, 2004
    #10
  11. m sergei

    Rolf Magnus Guest

    JKop wrote:

    >
    > int& k = ... ;
    >
    > (&k)[5] = 34;


    You don't actually use the reference for indexing, but rather take the
    pointer to the int it refers to (&k) and use that. Sure, you could also
    do:

    int& k = *new int[100];
    (&k)[5] = 34;
    delete &k;

    but it's not very useful or intuitive.
     
    Rolf Magnus, Jul 1, 2004
    #11
  12. On Thu, 01 Jul 2004 13:39:55 GMT, JKop <> wrote:

    >
    >int& k = ... ;
    >
    >(&k)[5] = 34;
    >
    >
    >-JKop


    What is that supposed to illustrate?

    --
    Bob Hairgrove
     
    Bob Hairgrove, Jul 1, 2004
    #12
  13. m sergei

    JKop Guest

    Bob Hairgrove posted:

    > On Thu, 01 Jul 2004 13:39:55 GMT, JKop <> wrote:
    >
    >>
    >>int& k = ... ;
    >>
    >>(&k)[5] = 34;
    >>
    >>
    >>-JKop

    >
    > What is that supposed to illustrate?
    >
    > --
    > Bob Hairgrove
    >



    Well it's more entertaining than the "variable" conversation that's going
    on.

    -JKop
     
    JKop, Jul 1, 2004
    #13
  14. m sergei

    Rolf Magnus Guest

    JKop wrote:

    > Well it's more entertaining than the "variable" conversation that's
    > going on.


    So you're here for entertainment? If you want that, you should try any
    newsgroup that contains "advocacy" in its name.
     
    Rolf Magnus, Jul 1, 2004
    #14
  15. [TROLL ALERT] Re: reference vs pointer .. ?

    On Thu, 01 Jul 2004 16:31:40 GMT, JKop <> wrote:

    >Bob Hairgrove posted:
    >
    >> On Thu, 01 Jul 2004 13:39:55 GMT, JKop <> wrote:
    >>
    >>>
    >>>int& k = ... ;
    >>>
    >>>(&k)[5] = 34;
    >>>
    >>>
    >>>-JKop

    >>
    >> What is that supposed to illustrate?
    >>
    >> --
    >> Bob Hairgrove
    >>

    >
    >
    >Well it's more entertaining than the "variable" conversation that's going
    >on.
    >
    >-JKop


    If this isn't trolling, tell me, what is??

    *plonk*

    --
    Bob Hairgrove
     
    Bob Hairgrove, Jul 1, 2004
    #15
  16. m sergei

    Apokrif Guest

    m. sergei :

    > Question is about the difference between these two
    >
    > reference to an object versus pointer to an object
    >
    > What are advantages in either one and when would one use them. I see
    > lots of examples when an object is passed as a parameter to a
    > function it is passed as a reference. Though it would have been fine
    > to pass it as a pointer as well ? is that right ?


    I found two pages on this topic:

    http://www.seasite.niu.edu/cs240/CPP_Notes/Notes9.htm
    http://www.phptr.com/articles/article.asp?p=31783&seqNum=3

    According to the latter, the use of references is handier because the
    program is easier to read and write, but this is only a cosmetic
    difference.

    --
    `cat ~/.signature`
     
    Apokrif, Jul 1, 2004
    #16
  17. m sergei

    JKop Guest

    Re: [TROLL ALERT] Re: reference vs pointer .. ?

    Bob Hairgrove posted:

    > If this isn't trolling, tell me, what is??
    >
    > *plonk*
    >
    > --
    > Bob Hairgrove
    >
    >



    This.


    -JKop
     
    JKop, Jul 1, 2004
    #17
  18. m sergei

    jeffc Guest

    "m sergei" <> wrote in message
    news:...
    > Question is about the difference between these two
    >
    > reference to an object versus pointer to an object
    >
    > What are advantages in either one and when would one use them. I see
    > lots of examples when an object is passed as a parameter to a function
    > it is passed as a reference. Though it would have been fine to pass it
    > as a pointer as well ? is that right ?


    Basically, a pointer is more complicated than you need. Use a reference
    when you can, and a pointer only when a reference won't work.
     
    jeffc, Jul 1, 2004
    #18
  19. m sergei

    m sergei Guest

    say class is Test
    then
    Test *t=new Test();
    t->a=9; //to access a member of class Test


    how would the & reference work here (instead of *t that was used above)

    like Test &t = new Test() //gives compile error

    please give the correct usage with & (reference of object)
     
    m sergei, Jul 7, 2004
    #19
  20. m sergei

    Buster Guest

    m sergei wrote:

    > say class is Test
    > then
    > Test *t=new Test();
    > t->a=9; //to access a member of class Test
    >
    >
    > how would the & reference work here (instead of *t that was used above)
    >
    > like Test &t = new Test() //gives compile error
    >
    > please give the correct usage with & (reference of object)


    Test & t = * new Test ();
    t.a = 9;
    delete & t;

    --
    Regards,
    Buster.
     
    Buster, Jul 7, 2004
    #20
    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. sam pal
    Replies:
    3
    Views:
    550
    E. Robert Tisdale
    Jul 16, 2003
  2. Replies:
    10
    Views:
    704
    Chris Torek
    Feb 4, 2005
  3. Replies:
    4
    Views:
    399
    Andre Kostur
    Dec 24, 2007
  4. mathieu
    Replies:
    8
    Views:
    512
    Juha Nieminen
    Aug 31, 2008
  5. A
    Replies:
    7
    Views:
    639
Loading...

Share This Page