Can #39;iterator#39; type just subclass #39;const_iterator#39;?(iterator 可以只键入const_iterator 的子类吗?)
问题描述
在关于迭代器的另一个问题之后我对自定义容器有一些疑问.在我的容器中,iterator
是 const_iterator
的子类,因此我可以免费"从非 const 转换为 const.但这是否允许,或者这样的设置是否有任何缺点或不工作的情况?
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?
推荐答案
是的,这很好.例如,这就是 VC10 的 vector
迭代器实现的结构.请参阅
中的 _Vector_iterator
和 _Vector_const_iterator
.
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>
.
顺便说一句,编写迭代器很困难.值得您花时间学习和使用 boost::iterator 库.
By the way, writing iterators is hard. It's worth your time to learn and use the boost::iterator library.
这篇关于'iterator' 可以只键入'const_iterator' 的子类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:'iterator' 可以只键入'const_iterator' 的子类吗?


基础教程推荐
- C语言数组 1970-01-01
- 用指数格式表示浮点数 1970-01-01
- 明确指定任何或所有枚举数的整数值 1970-01-01
- 总计将在节日礼物上花多少钱 1970-01-01
- C++多态 1970-01-01
- 向量<unique_ptr<A>>使用初始化列表 2022-10-23
- C++:为什么结构类需要一个虚拟方法才能成为多态? 2022-10-19
- 迭代std :: bitset中真实位的有效方法? 2022-10-18
- C语言3个整数的数组 1970-01-01
- 对 STL 容器的安全并行只读访问 2022-10-25