从STL迭代器类继承通常是否安全? [英] Is it generally safe to inherit from STL iterator classes?

查看:54
本文介绍了从STL迭代器类继承通常是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解为什么从STL容器继承是不安全的,但我发现了(在SGI STL文档中)
,例如bidirectional_iterator

class可以使用通过继承

来创建自己的迭代器类,即。


class my_bidirectional_iterator:public bidirectional_iterator< double>

{

...

};


我正在尝试为我的自定义类实现STL兼容的迭代器但是我

需要通过解除引用我的迭代器来更改返回的类型,并且从bidirectional_iterator继承

会使我的工作变得更加容易。现在,

我想知道,我是否需要使用SGI STL安全地执行此操作,或者它是STL的标准行为?




我发现很少有关于创建自定义迭代器的教程,我想我会理解如何做到这一点,但在开始之前,我想知道它是否值得

(安全)这样做,教程中没有任何内容可以解释...


TA

解决方案
" TA" <到************* @ gmail.comwrote:


我理解为什么从STL容器继承是不安全的,但是我发现了(在SGI STL文档中)
,例如bidirectional_iterator

类可以通过继承
$ b $来创建自己的迭代器类b it,即。


class my_bidirectional_iterator:public bidirectional_iterator< double>

{

...

};


我正在尝试为我的自定义类实现STL兼容的迭代器,但是我需要通过解除引用我的迭代器来更改返回的类型并且从bidirectional_iterator继承

会让我的工作变得更加容易。现在,

我想知道,我是否需要使用SGI STL来安全地执行此操作,或者它是STL的标准行为?



Bidirectional_iterator没有成员函数,成员变量或

嵌套类型。它仅用于简化

函数iterator_category,distance_type和value_type的定义。


bidirectional_iterator存在的唯一原因是你没有

将三个typedef放入你的班级。所以继承。不要从std :: vector :: iterator继承




-

有两件事根本无法做到怀疑,逻辑和感知。

怀疑那些,你不再有任何人讨论你的怀疑,

或任何讨论它们的能力。


Daniel T.写道:


" TA" <到************* @ gmail.comwrote:


>我理解为什么从STL继承是不安全的容器,但是我已经找到了(在SGI STL文档中),例如
bidirectional_iterator类可以通过继承它来创建自己的迭代器类,即

class my_bidirectional_iterator:public bidirectional_iterator< double>
{
...
};

我正在尝试为我的自定义实现STL兼容的迭代器但是我需要通过解除引用我的迭代器来改变返回的类型,并且继承自bidirectional_iterator会使我的工作变得更加容易。现在,我想知道,我是否需要使用SGI STL安全地执行此操作,或者它是STL的标准行为?



Bidirectional_iterator没有成员函数,成员变量或

嵌套类型。它仅用于简化

函数iterator_category,distance_type和value_type的定义。


bidirectional_iterator存在的唯一原因是你没有

将三个typedef放入你的班级。所以继承。不要从std :: vector :: iterator继承




嗯?我刚刚发现自己从std :: set :: const_iterator继承了另一天

天,我没有看到它有什么问题。从非多态类继承的标准参数

不适用:你不能多态地使用

std :: vector :: iterator *和函数

std :: vector :: iterator&作为参数,所有应该在迭代器上模板化

类型(以及来自STL的那些)。你能详细说明吗?


具体来说,我所做的代码就在这篇文章中:

http://groups.google.com/group/comp....5246c3ffbb9cfc

它定义了一个跟踪的政策< Tto be as


类TrackedClass:公共跟踪< TrackedClass {

...

}


并自动添加到TrackedClass功能的接口

迭代当前存在的TrackedClass类型的所有对象。

这个逻辑的基础是静态std :: set<跟踪*对象,因为投射

到TrackedClass *跟踪不可用< TrackedClass>。因此,提供了一个

迭代器类型,其操作符*执行必要的上传。

最佳


Kai-Uwe Bux


2006年9月23日星期六11:12:44 -0400,Kai-Uwe Bux写道:


Daniel T.写道:


...


嗯?我刚刚发现自己从std :: set :: const_iterator继承了另一天

天,我没有看到它有什么问题。从非多态类继承的标准参数

不适用:你不能使用

std :: vector :: iterator *多态



好​​的,这是有道理的......没有理由多态地使用迭代器...


但是我想知道这样的工作会不会很好和安全(从

视图的STL算法):


class SomeClass;


class MyClass {$ / $

私人:

typedef SmartPointer< SomeClasssmart_pointer;

typedef std :: vector< smart_pointersmart_pointer_vector;


smart_pointer_vector itsContents;


public:

class iterator:public smart_pointer_vector :: iterator {

.. 。

};


iterator begin(){return itsContents.begin(); }

iterator end(){return itsContents.end(); }

}


现在,当某人使用时,例如,在MyClass上查找算法,它将按预期工作
,或者有一些我不知道的危险吗? (我认为

它会起作用,但我仍然是C ++的新手,所以我决定仔细检查)


TIA


I understand why it is not safe to inherit from STL containers, but I have
found (in SGI STL documentation) that for example bidirectional_iterator
class can be used to create your own iterator classes by inheriting from
it, ie.

class my_bidirectional_iterator : public bidirectional_iterator<double>
{
...
};

I''m trying to implement STL compatible iterators for my custom class but I
need to change type returned by dereferencing my iterators, and inheriting
from bidirectional_iterator would make my job much easier of course. Now,
what I''m wondering, do I need to use SGI STL to do that safely, or it is
standard behavior for STL?

I have found few tutorials about creating custom iterators and I think I
understand how to do it, but before I start, I want to know is it worth
(safe) to do it and there is nothing in tutorials that explains that...

T.A.

解决方案

"T.A." <to*************@gmail.comwrote:

I understand why it is not safe to inherit from STL containers, but I have
found (in SGI STL documentation) that for example bidirectional_iterator
class can be used to create your own iterator classes by inheriting from
it, ie.

class my_bidirectional_iterator : public bidirectional_iterator<double>
{
...
};

I''m trying to implement STL compatible iterators for my custom class but I
need to change type returned by dereferencing my iterators, and inheriting
from bidirectional_iterator would make my job much easier of course. Now,
what I''m wondering, do I need to use SGI STL to do that safely, or it is
standard behavior for STL?

Bidirectional_iterator has no member functions, member variables, or
nested types. It exists solely to simplify the definition of the
functions iterator_category, distance_type, and value_type.

The only reason bidirectional_iterator exists is so you don''t have to
put the three typedefs in your class. So inherit away. Do not inherit
from std::vector::iterator though.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer have anyone to discuss your doubts with,
nor any ability to discuss them.


Daniel T. wrote:

"T.A." <to*************@gmail.comwrote:

>I understand why it is not safe to inherit from STL containers, but I
have found (in SGI STL documentation) that for example
bidirectional_iterator class can be used to create your own iterator
classes by inheriting from it, ie.

class my_bidirectional_iterator : public bidirectional_iterator<double>
{
...
};

I''m trying to implement STL compatible iterators for my custom class but
I need to change type returned by dereferencing my iterators, and
inheriting from bidirectional_iterator would make my job much easier of
course. Now, what I''m wondering, do I need to use SGI STL to do that
safely, or it is standard behavior for STL?


Bidirectional_iterator has no member functions, member variables, or
nested types. It exists solely to simplify the definition of the
functions iterator_category, distance_type, and value_type.

The only reason bidirectional_iterator exists is so you don''t have to
put the three typedefs in your class. So inherit away. Do not inherit
from std::vector::iterator though.

Huh? I just found myself inheriting from std::set::const_iterator the other
day, and I don''t see anything wrong with it. The standard arguments agains
inheriting from non-polymorphic classes do not apply: you don''t use
std::vector::iterator* polymorphically and the functions taking
std::vector::iterator& as arguments all should be templated on an iterator
type anyway (and the ones from STL are). Could you please elaborate?

For concreteness, the code where I did it, is in this posting:

http://groups.google.com/group/comp....5246c3ffbb9cfc

It defines a policy tracked<Tto be used as

class TrackedClass : public tracked<TrackedClass{
...
}

and automatically adds to the interface of TrackedClass functionality to
iterate over all objects of type TrackedClass that currently exist.
Underlying this logic is a static std::set< tracked* object since casting
to TrackedClass* is not available in tracked< TrackedClass >. Thus, an
iterator type is provided whose operator* performs the necessary up-cast.
Best

Kai-Uwe Bux


On Sat, 23 Sep 2006 11:12:44 -0400, Kai-Uwe Bux wrote:

Daniel T. wrote:

...

Huh? I just found myself inheriting from std::set::const_iterator the other
day, and I don''t see anything wrong with it. The standard arguments agains
inheriting from non-polymorphic classes do not apply: you don''t use
std::vector::iterator* polymorphically

OK, this makes a sense... No reason to use iterators polymorphically...

But I wonder will something like this work nice and safe (from the point of
view of STL algorithms):

class SomeClass;

class MyClass {

private:
typedef SmartPointer<SomeClasssmart_pointer;
typedef std::vector<smart_pointersmart_pointer_vector;

smart_pointer_vector itsContents;

public:
class iterator : public smart_pointer_vector::iterator {
...
};

iterator begin() { return itsContents.begin(); }
iterator end() { return itsContents.end(); }
}

Now, when somebody uses, for example, find algorithm on MyClass will it
work as expected, or there are some dangers that I''m not aware of? (I think
it will work but I''m still quite new to C++ so I decided to double check)

TIA


这篇关于从STL迭代器类继承通常是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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