Writing my own implementation of stl-like Iterator in C++

前端 未结 2 643
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 01:14

I\'m currently trying to understand the intrinsics of iterators in various languages i.e. the way they are implemented.

For example, there is the following class ex

2条回答
  •  暖寄归人
    2021-01-02 01:55

    STL doesn't really employ abstract base classes and virtual functions. Instead it is knowingly designed not to be OO (in the sense of GoF) and built entirely on templates, aiming for "compile-time polymorphism". Templates don't care about abstract interfaces. Things work as long as they have a sufficiently similar interface (e.g if you were to call Append push_back instead, more code expecting STL compliant containers would work for you, such as std::back_insert_iterator).

    A STL compliant iterator would have to overload lots of operators to behave like a pointer (as far as possible, given the limitations of the container), including *, ->, ++, -- (if bidirectional - doubly linked), == and !=.

提交回复
热议问题