How do I overload the dereference operator? What would the declaration look like? I'm creating a list class, but I am have trouble with the dereference operator.
Here is my function for overloading the dereference operator
template <typename T>
T List_Iterator<T>::operator *(){
return current_link->value;
}
This is the data members in my iterator class
private:
/* Data Members */
Link<T>* current_link;
This is my link class
protected:
T value;
You should be returning a reference, not a copy:
T& List_Iterator<T>::operator *() { .... }
Otherwise the semantics are confusing: you could not modify the object that you are "de-referencing".
You can also return by address. It's easier to write a->b
than (*a).b
:
T* operator->() {
return ¤t_link->value;
}
You will certainly need a const-version of your iterator, that will basically do the same thing as the first one, with the const
versions of the "dereferencing" operators.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.