difference between return &*i and return i;

Discussion in 'C++' started by Ganesh Gella, Nov 11, 2004.

  1. Ganesh Gella

    Ganesh Gella Guest

    Hi All,

    At some places in code, I see following statements,

    const XMLCh* some_func()
    {
    return &*(*i)->m_Value.begin();
    }

    Is that any different from following syntax ?

    const XMLCh* some_func()
    {
    return (*i)->m_value.begin();
    }

    Thanks
    Ganesh.
     
    Ganesh Gella, Nov 11, 2004
    #1
    1. Advertising

  2. Ganesh Gella wrote:
    > At some places in code, I see following statements,
    >
    > const XMLCh* some_func()
    > {
    > return &*(*i)->m_Value.begin();
    > }
    >
    > Is that any different from following syntax ?
    >
    > const XMLCh* some_func()
    > {
    > return (*i)->m_value.begin();
    > }
    > ...


    'i' could easily be different from '&*i' if the '*' operator is
    overloaded in 'i'. That appears to be the case in the code above.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 11, 2004
    #2
    1. Advertising

  3. Ganesh Gella

    Ron Natalie Guest

    Ganesh Gella wrote:
    > Hi All,
    >
    > At some places in code, I see following statements,
    >
    > const XMLCh* some_func()
    > {
    > return &*(*i)->m_Value.begin();
    > }
    >


    If the value returned by begin() has an overload for operator*
    (or the result of *begin() has an overload for &), it's not the
    same as omitting the &*.

    For example if m_Value is list<XMLCh> type, then begin() returns
    list<XLMCh>::iterator which is NOT the same as XMLCh*. It does
    however have an operator* that reutrns XMLCh& and you can take the
    address of that to get the adderss of the XMLCh object in the list.
     
    Ron Natalie, Nov 11, 2004
    #3
  4. "Ganesh Gella" <> wrote in message
    news:...
    > Hi All,
    >
    > At some places in code, I see following statements,
    >
    > const XMLCh* some_func()
    > {
    > return &*(*i)->m_Value.begin();
    > }
    >
    > Is that any different from following syntax ?
    >
    > const XMLCh* some_func()
    > {
    > return (*i)->m_value.begin();
    > }
    >
    > Thanks
    > Ganesh.


    Yes, the return from begin() is (presumably) an iterator, and the return
    from &* ... begin() is (presumably) a pointer.

    john
     
    John Harrison, Nov 12, 2004
    #4
  5. (Ganesh Gella) wrote in message news:<>...
    > Hi All,
    >
    > At some places in code, I see following statements,
    >
    > const XMLCh* some_func()
    > {
    > return &*(*i)->m_Value.begin();
    > }
    >
    > Is that any different from following syntax ?
    >
    > const XMLCh* some_func()
    > {
    > return (*i)->m_value.begin();
    > }
    >
    > Thanks
    > Ganesh.


    outside of operator loading, yes. but with operator overloading it can be anything
     
    Stuart Gerchick, Nov 12, 2004
    #5
    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,280
  2. Santa
    Replies:
    1
    Views:
    1,096
    Mark A. Odell
    Jul 17, 2003
  3. w i l l
    Replies:
    4
    Views:
    245
    Dan Brussee
    Jul 4, 2003
  4. PerlFAQ Server
    Replies:
    0
    Views:
    367
    PerlFAQ Server
    Jan 6, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    272
    PerlFAQ Server
    Apr 15, 2011
Loading...

Share This Page