How to overload the indirection operator? (C++)

后端 未结 2 1485
既然无缘
既然无缘 2021-01-12 01:30

I\'m trying to create an iterator class as a member-class for a list class, and am trying to overload the indirection operator (*) to access the list it\'s pointing to:

2条回答
  •  既然无缘
    2021-01-12 01:54

    You overloaded the multiply operator. Take the parameter out to make it an indirection operator.

    template
    T list::iterator::operator*()
    {
        return ((this->lstptr)->current)->data;
    }
    

    You should also have it return a reference if you want code like *IT = 3; to compile.

    template
    T& list::iterator::operator*()
    {
        return ((this->lstptr)->current)->data;
    }
    

提交回复
热议问题