从迭代器派生? [英] Deriving from an iterator?
问题描述
是否可以保证STL迭代器是聚合类型,因此可以从
派生出来?
我正在思考,因为例如:
类DerIter:public std :: vector< int> :: iterator
{...};
我关心的是标准是否允许实现使用
a裸指针作为迭代器。
谢谢,
Mark
2006-02-17 13:53:22 -0500,Mark P
< us****@fall2005REMOVE.fastmailCAPS.fm>说:
是否保证STL迭代器是聚合类型,因此可以从中派生出来?
No.
我正在思考,例如:
类DerIter:public std :: vector< int> :: iterator
{...};
我关心的是标准是否允许实现使用裸指针作为迭代器。
是的,它允许它(许多实现都使用原始指针)。
例如,没有什么能阻止std :: vector<来自
的T> :: iterator属于(T *)类型。
此外,即使迭代器是聚合的,也可能没有一个
虚拟析构函数。
你想要实现的目标是什么?使用
a有一段关系并提供转换运算符?
-
Clark S. Cox,III
cl ******* @ gmail.com
周五,17 2006年2月18:53:22 GMT,Mark P
< us **** @ fall2005REMOVE.fastmailCAPS.fm>写道:是否保证STL迭代器是聚合类型,因此可以从中派生出来?
不,迭代器可能是一个很好的老指针。
我正在思考,例如:
class DerIter:public std :: vector< int> :: iterator
{...};
我关心的是标准是否允许实现使用
一个裸指针作为迭代器。
你为什么要这样做?你的迭代器不会是多元的
无论如何。
祝福,
Roland Pibinger
Clark S. Cox III写道:
此外,即使迭代器是聚合的,它也可能没有一个
虚拟析构函数。
因为通常的习惯用法是按值传递迭代器,所以这不会产生
的差异。我想不出你想要删除
迭代器的情况。
-
Pete Becker
Roundhouse Consulting,Ltd。
Is it guaranteed that STL iterators are aggregate types and can thus be
derived from?
I''m thinking along the lines of, for example:
class DerIter : public std::vector<int>::iterator
{...};
My concern is whether the standard might allow an implementation to use
a bare pointer as an iterator.
Thanks,
Mark
On 2006-02-17 13:53:22 -0500, Mark P
<us****@fall2005REMOVE.fastmailCAPS.fm> said:
Is it guaranteed that STL iterators are aggregate types and can thus be
derived from?
No.
I''m thinking along the lines of, for example:
class DerIter : public std::vector<int>::iterator
{...};
My concern is whether the standard might allow an implementation to use
a bare pointer as an iterator.
Yes, it does allow it (and many implementations do use raw pointers).
For example, there is nothing preventing std::vector<T>::iterator from
being of type (T*).
Additionally, even if the iterator is aggregate, it likely won''t have a
virtual destructor.
What are you trying to accomplish that cannot be accomplished by using
a has-a relationship and providing a conversion operator?
--
Clark S. Cox, III
cl*******@gmail.com
On Fri, 17 Feb 2006 18:53:22 GMT, Mark P
<us****@fall2005REMOVE.fastmailCAPS.fm> wrote:Is it guaranteed that STL iterators are aggregate types and can thus be
derived from?
No, an iterator may be a good old pointer.
I''m thinking along the lines of, for example:
class DerIter : public std::vector<int>::iterator
{...};
My concern is whether the standard might allow an implementation to use
a bare pointer as an iterator.
Why would you want to do that? You iterator wouldn''t be polymorohic
anyway.
Best wishes,
Roland Pibinger
Clark S. Cox III wrote:
Additionally, even if the iterator is aggregate, it likely won''t have a
virtual destructor.
Since the usual idiom is to pass iterators by value, this makes no
difference. I can''t think of a situation where you''d want to delete an
iterator.
--
Pete Becker
Roundhouse Consulting, Ltd.
这篇关于从迭代器派生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!