问题:如何使用一个std容器来存储多种类型? [英] Problem: how to use one std container to store multiple types?

查看:96
本文介绍了问题:如何使用一个std容器来存储多种类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

最重要的是,请原谅我的英语不好和非常新手

问题。我希望你不要无聊。


我正在尝试使用openGL编写一个非常简单的GUI。

所以我正在写作一些不同的小部件类,如按钮,图像,

滑块等...


每个类都有一个draw()方法。


想法是将所有小部件都放在一个向量中,然后用伪代码:


------------ -----------------
每个小部件w的
向量:

w.draw()

-----------------------------


所以我应该声明向量,像


vector< widget> widgetList;


这是我的麻烦:我有不同的类(有一些常见的

方法,比如draw(),on(), off()等...)我应该把所有的一个放在一个

std容器中。这有可能吗?

真的我甚至不知道如何声明矢量。例如,如果我有一个
这个班级


-------------------- --------

class foo {

private:

int width;

int height ;

public:

int getWidth();

int getHeight();

};

----------------------------


如何申报容器为了foo?肯定不写作


vector< foo> fooList;


我应该使用typedef吗?你能解释一下吗?


我知道我是新手,也许这些都是非常愚蠢的问题。但是我不能在我正在阅读的书上找到这些信息(它们适合初学者,

而且不写关于性病的信息)和同样使用谷歌。

你能帮帮我吗?


谢谢,

曼努埃尔

Hi.
Before all, please excuse me for bad english and for very newbie
questions. I hope to don''t boring you.

I''m trying to write a very simple GUI using openGL.
So I''m writing some different widgets classes, like buttons, images,
slider, etc...

Each class has a draw() method.

The idea is to put all widget to draw in a vector, and after, in pseudocode:

-----------------------------
for each widget w in vector:
w.draw()
-----------------------------

So I should declare the vector, like

vector<widget> widgetList;

and this is my trouble: I''ve different classes (that has some common
methods, like draw(), on(), off() etc...) and I should put all in one
std container. It''s possible?
Really I don''t know even how to declare the vector. In example, if I''ve
the class

----------------------------
class foo {
private:
int width;
int height;
public:
int getWidth();
int getHeight();
};
----------------------------

how to declare a container for foo? Surely not writing

vector<foo> fooList;

Should I use typedef? Can you explain me this?

I know I''m a newbie and maybe these are very silly questions. However I
can''t find these info on the books I''m reading (they are for beginners,
and don''t write about STD) and the same using google.
Can you help me?

Thanks,
Manuel

推荐答案

Manuel写道:

我正在尝试使用openGL编写一个非常简单的GUI。所以我正在写一些不同的小部件类,比如按钮,图像,滑块等......

每个类都有一个draw()方法。
我的想法是将所有小部件都放在一个向量中,之后,在
伪代码中:

------------------ -----------对于vector中的每个小部件w:w.draw()
----------------------- ------


查看名为Composite的Design Pattern,它可以轻松地包含这样的

结构。 GoF的书[1]包括与你的问题非常相似的案例研究,所以你可能会发现这本书很有用。


简单地说,创建抽象基类即Widget和

从Widget继承具体(专用)小工具。

在Widget中声明纯虚拟方法即void draw(画布)

和适当地在每个具体的小部件中实现它。

最后,通过指向基础的指针将所有小部件存储在容器中

类小部件(动态多态)。


所以我应该声明向量,例如

vector< widget> widgetList;

这是我的麻烦:我有不同的类(有一些常见的方法,比如draw(),on(),off()等......)我应该把所有的东西放在一个std容器里。这可能吗?

I''m trying to write a very simple GUI using openGL. So I''m writing
some different widgets classes, like buttons, images, slider, etc...

Each class has a draw() method.

The idea is to put all widget to draw in a vector, and after, in
pseudocode:

----------------------------- for each widget w in vector: w.draw()
-----------------------------

Look at Design Pattern called Composite which can be used to wrap such
structure easily. GoF''s book [1] includes case study very similar to
your problem so you may find this book intersting.

Simply, create abstract base class i.e. Widget and
inherit concrete (specialized) gadgets from the Widget.
Declare in Widget pure virtual method i.e. void draw(canvas)
and implement it in every concrete widget suitably.
Finally, store all widgets in a container through pointer to the base
class Widget (dynamic polymorphism).

So I should declare the vector, like

vector<widget> widgetList;

and this is my trouble: I''ve different classes (that has some common
methods, like draw(), on(), off() etc...) and I should put all in
one std container. It''s possible?




是的,就像我上面解释的那样。

这是一个简单的实现,应该有助于理解这个想法:


#include< algorithm>

#include< iostream>

#include< vector>

///////////////////////////////////////////// ///////////////

struct DeleteObject

{

template< typename T>

void operator()(const T * ptr)const

{

delete ptr;

}

};

//////////////////////////////////// ////////////////////////

级小工具

{

public:

virtual void draw()const = 0;

virtual~Widget(){}

};

class Button:public Widget

{

public:

void draw()const {std :: cout<< 绘图按钮 <<的std :: ENDL; }

};

class标签:public Widget

{

public:

void draw()const {std :: cout<< 绘图标签 <<的std :: ENDL; }

};

class Slider:public Widget

{

public:

void draw()const {std :: cout<< 绘图滑块 <<的std :: ENDL; }

};


int main()

{

std :: vector< Widget * GT; v;

v.push_back(new Button());

v.push_back(new Label());

v.push_back(新的Slider());

v.push_back(新的Button());


std :: vector< Widget *> :: const_iterator it;

for(it = v.begin(); it!= v.end(); ++ it)

{

(*它) - > draw();

}


//删除小部件

std :: for_each(v.begin) (),v.end(),DeleteObject());


返回0;

}

//// ////////////////////////////////////////////////// //////


[1]设计模式:可重复使用的面向对象软件的元素

由Erich Gamma,Richard Helm,Ralph Johnson,John Vlissides


干杯

-

Mateusz ?? oskot
http://mateusz.loskot.net


Mateusz ?? oskot写道:
Mateusz ??oskot wrote:
查看名为Composite的Design Pattern可以用来轻松包装这样的结构。 GoF的书[1]包含非常类似于你的问题的案例研究,所以你可能会发现这本书很有用。

简单地说,创建抽象基类,即Widget和
继承来自Widget的具体(专用)小工具。
在Widget中声明纯虚拟方法即void draw(canvas)
并在每个具体小部件中适当地实现它。
最后,将所有小部件存储在容器中通过指向基类
类Widget(动态多态)的指针。
Look at Design Pattern called Composite which can be used to wrap such
structure easily. GoF''s book [1] includes case study very similar to
your problem so you may find this book intersting.

Simply, create abstract base class i.e. Widget and
inherit concrete (specialized) gadgets from the Widget.
Declare in Widget pure virtual method i.e. void draw(canvas)
and implement it in every concrete widget suitably.
Finally, store all widgets in a container through pointer to the base
class Widget (dynamic polymorphism).




非常感谢你的例子!!


一些问题:


1)如果我使用v.clear()代替DeleteObject()会发生什么?

2)在你的例子中每个class具有相同数量的方法。但是如果某些类有不同数量的方法,会发生什么?b $ b?应该不是

一个问题,对吧?

3)存在其他方式而不是在std :: vector< Widget *>中使用指针v; ??


再次,谢谢,谢谢!


Ciao,


Manuel


Manuel写道:

非常感谢你的榜样!!


你' 欢迎!

一些问题:

1)如果我使用v.clear()而不是DeleteObject()会发生什么?


查看标题为STL地图和内存管理(清除())的旧版主题,

约12月15日。

2)在您的示例中,每个类具有相同数量的方法。但是如果某些类有不同数量的方法会发生什么?应该不是问题吧?


没问题。但请记住,在
迭代期间只能访问普通成员(没有强制转换)。我的意思是,在迭代时你可以看到所有

小部件通过基类接口 - 小部件。

只是一个比喻性的解释:-)

3)存在其他方式而不是在std :: vector< Widget *>中使用指针v;
??

Thanks you very much for the example!!

You''re welcome!
Some questions:

1) What happen if I use v.clear() instead DeleteObject() ??
Look at older thread titled "STL map and memory management (clear() )",
about Dec, 15.
2) In your example each class has the same number of methods. But
what happen if some classes have different numbers of methods? Should
be not a problem, right ??
No problem. But remember that only common members may be accessed during
iterations (without casting). I mean, when iterating you "can see" all
widgets "through" base class interface - Widget.
Just a figurative explanation :-)
3) Exist other way instead using a pointer in std::vector<Widget*> v;
??




在你的特定解决方案中,我没有看到更好的东西 - 抽象基础

类允许你通过统一接口访问专用类型

(在基类中声明)。我强烈建议你阅读GoF'

对Composite的解释,然后你可能会得到它背后的想法

更清楚。

注意:您可以使用智能指针而不是原始指针。然后记忆

管理要简单得多。它们是:
http://www.boost。 org / libs / smart_ptr / smart_ptr.htm


干杯

-

Mateusz ?? oskot
http://mateusz.loskot.net



In your particular solution I don''t see anything better - abstract base
class allow you to access specialized types through unified interface
(declared in base class). I strongly recommend you to read GoF''s
explanation of Composite, then may be you will get the idea behind it
more clear.

Note: you can use "smart pointers" instead of raw pointers. Then memory
management is much simplier. Here they are:
http://www.boost.org/libs/smart_ptr/smart_ptr.htm

Cheers
--
Mateusz ??oskot
http://mateusz.loskot.net


这篇关于问题:如何使用一个std容器来存储多种类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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