Implementation of "->"

N

nafai3000

Hi. Do you know how is "operator->" implemented for STL iterators?

For example:

struct X {
int x;
...
};

std::list<X> l;
std::list<X>::iterator it = l.begin();
it->x = 1; // <-------------
 
V

Victor Bazarov

nafai3000 said:
Hi. Do you know how is "operator->" implemented for STL iterators?

Just look in their source, I am betting that you have the source to
the standard library along with your compiler.

The usual implementation is to return the address of the element the
iterator refers to.

V
 
B

Bushido Hacks

Actually -> is one of the operators you should not overload.

Keep in mind that the statement:
x->y
means
(*x).y

Such that the dot operator is another operator you can not overload.

The three operators you can not overload in C++ are:
:: (scope)
-> (arrow)
.. (dot)
 
B

ben

Keep in mind that the statement:
x->y
means
(*x).y

Not always true.
Such that the dot operator is another operator you can not overload.

The three operators you can not overload in C++ are:
:: (scope)
-> (arrow)
. (dot)

-> can be overloaded. They are useful for pointer like objects, such smart
pointers. For example, the auto_ptr class template implements operator ->.
In general, the following statement:

x->y

means (*x).y is x is a true pointer; (*(x.operator->())).y when x is not a
pointer at all.

ben
 

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

Forum statistics

Threads
474,293
Messages
2,571,506
Members
48,193
Latest member
DannyRober

Latest Threads

Top