Can'iterator'type只是子类'const_iterator'? [英] Can 'iterator' type just subclass 'const_iterator'?
问题描述
关于迭代器的另一个问题我对自定义容器有一些疑问。在我的容器中, iterator
是 const_iterator
的子类,所以我得到从非const转换为constfor自由。但是这是允许还是有这样的设置的任何缺点或非工作场景?
是的,这是好的。这是VC10如何实现向量
的迭代器的结构,例如。请参阅< vector>
中的 _Vector_iterator
和 _Vector_const_iterator
/ p>
顺便说一句,编写迭代器很困难。您值得花时间学习和使用boost :: iterator库。
After another question about iterators I'm having some doubts about custom containers. In my container, iterator
is a subclass of const_iterator
, so that I get conversion from non-const to const "for free". But is this allowed or are there any drawbacks or non-working scenarios for such a setup?
Yes, this is fine. This is how VC10's implementation of the iterators for vector
are structured, for example. See _Vector_iterator
and _Vector_const_iterator
in <vector>
.
By the way, writing iterators is hard. It's worth your time to learn and use the boost::iterator library.
这篇关于Can'iterator'type只是子类'const_iterator'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!