difference between pointer and reference

Discussion in 'C++' started by thomas, Jul 13, 2008.

  1. thomas

    thomas Guest

    I’m just writing a program which uses the queue stl type.



    Queue<packet*> queue_;

    Queue<packet&> queue_;



    These two writings are very similar, except that one is pointer and
    one is reference.

    Is there anything I must be careful when I am using either one of
    them?

    More specifically, is there anything really different between these
    two “queue”s?
     
    thomas, Jul 13, 2008
    #1
    1. Advertising

  2. On 2008-07-13 10:56, thomas wrote:
    > I’m just writing a program which uses the queue stl type.
    >
    >
    >
    > Queue<packet*> queue_;
    >
    > Queue<packet&> queue_;
    >
    >
    >
    > These two writings are very similar, except that one is pointer and
    > one is reference.
    >
    > Is there anything I must be careful when I am using either one of
    > them?


    Yes, only one of them is legal, you can not have a queue of references,
    you can either have a queue of pointers to packets or you can have a
    queue of packets, but not a queue of references to packets.

    --
    Erik Wikström
     
    Erik Wikström, Jul 13, 2008
    #2
    1. Advertising

  3. thomas

    thomas Guest

    On Jul 13, 5:44 pm, Erik Wikström <> wrote:
    > On 2008-07-13 10:56, thomas wrote:
    >
    > > I’m just writing a program which uses the queue stl type.

    >
    > > Queue<packet*>  queue_;

    >
    > > Queue<packet&>  queue_;

    >
    > > These two writings are very similar, except that one is pointer and
    > > one is reference.

    >
    > > Is there anything I must be careful when I am using either one of
    > > them?

    >
    > Yes, only one of them is legal, you can not have a queue of references,
    > you can either have a queue of pointers to packets or you can have a
    > queue of packets, but not a queue of references to packets.
    >
    > --
    > Erik Wikström


    why not? References just mean another name for these packet object.
    It's correct in my understanding.
    Any more details to clarify this?
     
    thomas, Jul 13, 2008
    #3
  4. On 2008-07-13 11:54, thomas wrote:
    > On Jul 13, 5:44 pm, Erik Wikström <> wrote:
    >> On 2008-07-13 10:56, thomas wrote:
    >>
    >> > I’m just writing a program which uses the queue stl type.

    >>
    >> > Queue<packet*> queue_;

    >>
    >> > Queue<packet&> queue_;

    >>
    >> > These two writings are very similar, except that one is pointer and
    >> > one is reference.

    >>
    >> > Is there anything I must be careful when I am using either one of
    >> > them?

    >>
    >> Yes, only one of them is legal, you can not have a queue of references,
    >> you can either have a queue of pointers to packets or you can have a
    >> queue of packets, but not a queue of references to packets.


    Please do not quota signatures.

    > why not? References just mean another name for these packet object.


    Since references are just another name for the object it is not an
    object in its own right (it does not occupy any memory and does not have
    an address*), which means you can not store it in a container.

    * At least not according to the C++ standard.

    --
    Erik Wikström
     
    Erik Wikström, Jul 13, 2008
    #4
  5. thomas

    rufus Guest

    "Erik Wikström" <> skrev i en meddelelse
    news:x2mek.957$...
    > On 2008-07-13 11:54, thomas wrote:
    >> On Jul 13, 5:44 pm, Erik Wikström <> wrote:
    >>> On 2008-07-13 10:56, thomas wrote:
    >>>
    >>> > I'm just writing a program which uses the queue stl type.
    >>>
    >>> > Queue<packet*> queue_;
    >>>
    >>> > Queue<packet&> queue_;
    >>>
    >>> > These two writings are very similar, except that one is pointer and
    >>> > one is reference.
    >>>
    >>> > Is there anything I must be careful when I am using either one of
    >>> > them?
    >>>
    >>> Yes, only one of them is legal, you can not have a queue of references,
    >>> you can either have a queue of pointers to packets or you can have a
    >>> queue of packets, but not a queue of references to packets.

    >
    > Please do not quota signatures.
    >
    >> why not? References just mean another name for these packet object.

    >
    > Since references are just another name for the object it is not an
    > object in its own right (it does not occupy any memory and does not have
    > an address*), which means you can not store it in a container.
    >
    > * At least not according to the C++ standard.



    Is a reference not basically just a const pointer?
     
    rufus, Jul 13, 2008
    #5
  6. On 2008-07-13 18:48, rufus wrote:
    > "Erik Wikstré—£" <> skrev i en meddelelse
    > news:x2mek.957$...
    >> On 2008-07-13 11:54, thomas wrote:
    >>> On Jul 13, 5:44 pm, Erik Wikstré—£ <> wrote:
    >>>> On 2008-07-13 10:56, thomas wrote:
    >>>>
    >>>> > I'm just writing a program which uses the queue stl type.
    >>>>
    >>>> > Queue<packet*> queue_;
    >>>>
    >>>> > Queue<packet&> queue_;
    >>>>
    >>>> > These two writings are very similar, except that one is pointer and
    >>>> > one is reference.
    >>>>
    >>>> > Is there anything I must be careful when I am using either one of
    >>>> > them?
    >>>>
    >>>> Yes, only one of them is legal, you can not have a queue of references,
    >>>> you can either have a queue of pointers to packets or you can have a
    >>>> queue of packets, but not a queue of references to packets.

    >>
    >> Please do not quota signatures.
    >>
    >>> why not? References just mean another name for these packet object.

    >>
    >> Since references are just another name for the object it is not an
    >> object in its own right (it does not occupy any memory and does not have
    >> an address*), which means you can not store it in a container.
    >>
    >> * At least not according to the C++ standard.

    >
    >
    > Is a reference not basically just a const pointer?


    That might be how the compiler vendors implements it (at least in some
    cases) but there are a number of semantic differences between references
    and const pointers. As an example a pointer can be a null-pointer while
    a reference always refers to a valid object, a const reference can bind
    to a temporary, a pointer can not.

    --
    Erik Wikström
     
    Erik Wikström, Jul 13, 2008
    #6
  7. thomas

    Stefan Ram Guest

    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= <> writes:
    >>Is a reference not basically just a const pointer?

    >That might be how the compiler vendors implements it (at least in some


    You might think of reference parameters only.
    But a reference also can be created as follows.

    { int v( 12 );
    int & w( v );

    Behind these two lines, there is no C++ statement you can write
    that will behave differently for »v« and »w«. So in this case,
    a reference is not like a pointer, but like a variable.
    »w« is /bound/ to the same object as »v«. (Both are not pointers.)

    I would go so far as to say, that here it makes no sense to
    call »w« a »reference«. It /is/ a variable, because it can not
    be distinguished from one after its creation.

    The difference is the type of creation: »v« is declared with a
    new object (with automatic storage duration), »w« is declared
    without a new object (as an alias for »v«, using an
    already-existing object).
     
    Stefan Ram, Jul 13, 2008
    #7
  8. thomas

    James Kanze Guest

    On Jul 13, 11:44 am, Erik Wikström <> wrote:
    > On 2008-07-13 10:56, thomas wrote:


    > > I’m just writing a program which uses the queue stl type.


    > > Queue<packet*> queue_;


    > > Queue<packet&> queue_;


    > > These two writings are very similar, except that one is
    > > pointer and one is reference.


    > > Is there anything I must be careful when I am using either
    > > one of them?


    > Yes, only one of them is legal,


    Actually, neither of them are legal. There isn't any class
    Queue in the standard. Of course, if he really means
    std::queue, then your comments are correct. (But of course, if
    he really means std::queue, that's what he should write.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 13, 2008
    #8
  9. thomas

    James Kanze Guest

    On Jul 13, 7:27 pm, Erik Wikström <> wrote:
    > On 2008-07-13 18:48, rufus wrote:
    > > "Erik Wikström" <> skrev i en meddelelse
    > >news:x2mek.957$...
    > >> On 2008-07-13 11:54, thomas wrote:
    > >>> On Jul 13, 5:44 pm, Erik Wikström <> wrote:
    > >>>> On 2008-07-13 10:56, thomas wrote:


    > >>>> > I'm just writing a program which uses the queue stl type.


    > >>>> > Queue<packet*> queue_;


    > >>>> > Queue<packet&> queue_;


    > >>>> > These two writings are very similar, except that one is
    > >>>> > pointer and one is reference.


    > >>>> > Is there anything I must be careful when I am using
    > >>>> > either one of them?


    > >>>> Yes, only one of them is legal, you can not have a queue
    > >>>> of references, you can either have a queue of pointers to
    > >>>> packets or you can have a queue of packets, but not a
    > >>>> queue of references to packets.


    > >>> why not? References just mean another name for these
    > >>> packet object.


    > >> Since references are just another name for the object it is
    > >> not an object in its own right (it does not occupy any
    > >> memory and does not have an address*), which means you can
    > >> not store it in a container.


    > >> * At least not according to the C++ standard.


    > > Is a reference not basically just a const pointer?


    > That might be how the compiler vendors implements it (at least
    > in some cases) but there are a number of semantic differences
    > between references and const pointers. As an example a pointer
    > can be a null-pointer while a reference always refers to a
    > valid object, a const reference can bind to a temporary, a
    > pointer can not.


    The critical point, of course, is that references are not
    objects, and a standard container can only contain objects. Or,
    the fact that references do not meet the requirements Assignable
    and CopyConstructable. Take your pick.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 13, 2008
    #9
    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. jakk
    Replies:
    4
    Views:
    12,600
  2. Replies:
    7
    Views:
    442
    Keith Thompson
    Oct 3, 2006
  3. Vols
    Replies:
    14
    Views:
    602
    Howard
    Aug 1, 2006
  4. A
    Replies:
    7
    Views:
    656
  5. somenath
    Replies:
    10
    Views:
    309
    James Kanze
    Jul 2, 2013
Loading...

Share This Page