The semantics of the operator & # 8594; in lists (and in general C ++)

My current job is writing a list with iterators. A list is not a problem since an iterator class is created.

From several sources, I saw that I have two operators for defining an iterator in my class: operator*and operator->.

Fine! Suppose my iterator structure is this:

// Nested class of List
class _Iter
{
private:
    ListElem *pCurr;
    const List *pList;

public:
    _Iter(ListElem *pCurr, const List *list)
        : pCurr_(pCurr), pList(list)
    {}

    T& operator*() { return pCurr_->data; }
    T* operator->() { return &**this; }
};

when ListElem is

// Nested struct of List
struct ListElem
{
    T data;

    ListElem *next;
    ListElem *prev;
};

I see that I am doing something massively wrong (since double dereferencing this will result in & (* pCurr _->) data, which is not unsolvable.

My main problem is not understanding what → should actually do in this case. Should it give the user access to the ListElem class? If so, why can't I just write

ListElem *operator->() { return pCurr_; }

? , (, , STL-), :

operator*() // Return data pointed to by iterator; pCurr_->data;
operator->() // Grant access to data-holding structure; pCurr; 

, ? ( -> ?)

+5
3

, (*something).somethingElse something->somethingElse. - . ,

T& operator*() { return pCurr_->data; }
T* operator->() { return &**this; }

, *this this, _Iter*, _Iter, operator*() . *this, pCurr->data, , & pCurr- > . :

T& operator*() { return pCurr_->data; }
T* operator->() { return &pCurr->data; }

ListElem *operator->() { return pCurr_; }

, operator*() T&, operator->() T*, , . ListItem ( , , ), operator*(),

ListElem& operator*() { return *pCurr_; }
ListElem *operator->() { return pCurr_; }

, , , .

+5

operator-> , , (-) pCurr_->data.

T *operator->() { return &(pCurr_->data); }

, operator*() .

T &operator*() { return pCurr_->data; }
// or
T &operator*() { return *operator->(); }

operator->() -> ( ) , operator* .

, List .

+1

,

(*iter).hello();
iter->hello();

. , . ListElem . ListElem.

+1

All Articles