Can/Should I inherit from an STL iterator?

后端 未结 4 1899
忘了有多久
忘了有多久 2020-12-08 04:34

Can/Should i inherit from STL iterator to implement my own iterator class? If no, why not?

4条回答
  •  误落风尘
    2020-12-08 04:59

    If you're talking about std::iterator template, then yes, you should, but I hope you do understand that it has no functionality, just a bunch of typedefs. The pro of this decision is that your iterator can be fed to iterator_traits template.

    If, on the other hand, you're talking about some specific STL iterator, like vector::iterator or other, then the answer is a resounding NO. Let alone everything else, you don't know for sure that it's actually a class (e.g. the same vector::iterator can be just typedefed as T*)

提交回复
热议问题