Can'iterator'type只是子类'const_iterator'? [英] Can 'iterator' type just subclass 'const_iterator'?

查看:155
本文介绍了Can'iterator'type只是子类'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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆