为自定义容器类创建迭代器 [英] Making an iterator for a custom container class

查看:41
本文介绍了为自定义容器类创建迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于结构的向量为一些不同类型的数据设计一个简单的容器类,但是vector和struct受到保护,因此我的容器类的实现可以独立于接口而改变。 br />

I''m trying to design a simple container class for some data of different types based on a vector of structs, but the vector and struct are protected so that the implemenation of my container class can change independently of the interface.

展开 | 选择 | Wrap | 行号

推荐答案

为什么在容器中声明结构?


我原本以为你会创建结构的对象并将它们添加到容器中。


通过保护,访问它被衍生为派生类。但是,这些派生类不是容器类型。


Yout类模型显示不正确。
Why is the struct declared in the container??

I would have thought you would have created objects of the struct and added them to the container.

By having ther strucr protected, access to it is restriceted to derived classes. Howver, these derived classes are not kinds of containers.

Yout class model appears incorrect.



为什么在容器中声明结构?
Why is the struct declared in the container??



没有要求先验数据存储在结构中。它可以存储在每个数据类型的一个向量中,或者完全存储在其他内容中;我的解决方案应该是独立于实现的。我们需要知道的是,我们将各种已知数据类型的记录存储在某个容器中,并在以后进行迭代。这就是为什么我认为最好使结构和向量受保护,只能访问(依赖于实现的)迭代器子类。你认为这是错误的吗?对于我天真的眼睛来说,这似乎是STL矢量库中发生的事情,而模板保存得非常普遍。


干杯,

Arnaud

There is no requirement a priori for the data to be stored in a struct. It may well be stored in one vector for each data type, or something else entirely; my solution should be implementation-independent. All we need to know is that we have a record of various known data types to be stored in some container and iterated over later. That''s why I thought it best to make the struct and vector protected, accessible only to an (implementation-dependent) iterator subclass. Do you think that''s going about it the wrong way? To my naive eye, it seems like that''s sort of what happens in the STL vector library while things are kept very general with templates.

Cheers,
Arnaud



你认为'这是错误的吗?
Do you think that''s going about it the wrong way?



我是。看起来你想要一个矢量< VARIANT>其中VARIANT是Microsoft区分的联合,用于在C / C ++和Visual Basic之间传递数据。它是针对面向对象问题的C结构化解决方案。


您的数据模型似乎需要:


容器

IntContainer:公共集装箱


DoubleContainer:公共集装箱:


等..


您可以在Container中使用公共函数来调用Container中的私有虚函数。这些私有虚函数是派生容器重写的函数。这将Container接口(公共函数)与Container实现(Container专用虚函数)分开。这是一种称为模板方法的设计模式。


大多数操作都可以使用容器接口。如果派生容器具有Container基类中不可用的特殊行为,则使用Visitor设计模式来执行thoise方法。在C / C ++文章论坛中有一篇关于Visitor的文章。


如果你这样做,你可以添加新类型的新容器,而不会影响currentlt编译代码。


参见Erich Gamma等人的设计模式一书。 ,Addision-Wesley 1994。

Yes I do. It appears you want a vector<VARIANT> where VARIANT is a Microsoft discriminated union used to pass data between C/C++ and Visual Basic. It is a C structured solution for an obect-oriented problem.

Your data model appears to need to be:

Container

IntContainer: public Container

DoubleContainer : public Container:

etc..

You use public functions in Container which call private virtual functions in the Container. These private virtual functions are the ones overridden by the derived containers. This separates the Container interface (the public functions) from the Container implementation (the Container private virtual functions. This is a design pattern called the Template Method.

Most operations can be performed using the Container interface. In cases where the derived container has special behaviors not available in the Container base class, you use a Visitor design pattern to execute thoise methods. There is an article on Visitor in the C/C++ articles forum.

If you go this way you can add new containers of new types without impacting currentlt compiled code.

See the book Design Patterns by Erich Gamma, et al., Addision-Wesley 1994.


这篇关于为自定义容器类创建迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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