Return correct iterator type for linked list

I implement a generic list: A list with two types of iterators: List<T>::Iterator and List<T>ConstIterator .

I have the following methods:

 typename List<T>::Iterator List<T>::begin() {} typename List<T>::ConstIterator List<T>::begin() const {} 

Is this a suitable way to return a non-constant iterator for a non-constant list and a constant iterator for a list of constants?

+4
source share
1 answer

Yes, just like in the standard library .

Like the standard library extensions introduced in C ++ 11, you can additionally provide the cbegin() function, which allows you to get a constant iterator, even if this list is not const.

+3
source

Source: https://habr.com/ru/post/1502853/


All Articles