'iterator' 可以只键入'const_iterator' 的子类吗? [英] Can 'iterator' type just subclass 'const_iterator'?

查看:38
本文介绍了'iterator' 可以只键入'const_iterator' 的子类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于迭代器的另一个问题之后我对自定义容器有一些疑问.在我的容器中,iteratorconst_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' 的子类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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