difference between return &*i and return i;

G

Ganesh Gella

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

Andrey Tarasevich

Ganesh said:
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.
 
R

Ron Natalie

Ganesh said:
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.
 
J

John Harrison

Ganesh Gella said:
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
 
S

Stuart Gerchick

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top