包含不同类型对象的双端队列(具有公共基类) [英] A deque containing different types of objects (with a common baseclass)

查看:58
本文介绍了包含不同类型对象的双端队列(具有公共基类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这不是一个新主意,但我以前从未听说过它。

之前。我想知道这是否可行:


假设你有一个共同的基类和一堆类

派生自它,你想要的制作一个可以包含任何类型的任何

对象的双端队列。通常你需要做的是

制作一个deque或基类类型的指针向量然后

用''new''动态分配每个对象将指针存储在

deque / vector中。这是一个麻烦,记忆漏洞,并且在很多情况下,与你有一个deque

包含一种类型的对象的情况相比,内存浪费了内存(特别是如果全部的话)您要存储的不同

对象的大小大致相等。


这个解决方案怎么样:


一个特殊的双端队列,可以在

中包含不同类型的对象,与std :: deque一样,可以存储相同类型的对象,但在这种情况下

它将是即使

对象的大小不同,也可以存储不同类型的对象。


唯一的额外要求是你必须告诉deque

将要存储在其中的最大对象的大小。


因此,通过为其提供基类类型来实例化此特殊双端队列

(将用于返回元素的引用/指针,

类似于基类类型的deque / vector pe指针可以工作)

和你将存储在其中的最大对象的大小(可以

也是模板参数)。我很确定使用模板

元编程甚至可以通过简单地列出你要使用的所有类来解决

最大类的大小在

某种模板构造。


这个双端队列将以相同的方式为元素分配空间

as std :: deque,但是为每个元素分配空间,给定

最大对象大小(而不是基类的大小)。

插入函数本身就是模板函数,并将使用

placement new将对象放在双端队列中的位置。由于

deque在创建之后永远不需要移动物体,

自我分配(其问题与deque有关只知道
$不需要b $ b基类。


当访问此双端队列时,它将返回基本

类类型的引用。然后用户可以使用它们做任何必要的事情(在

中,与使用基类指针向量的方式相同)。


并不是所有的std :: deque方法都可以提供,显然(例如

erase()是不可能实现的,因此它不会在

中提供所有)但在很多情况下都不重要。


这种方法的最大优点是你不需要担心
担心关于内存管理,如果你使用

传统,你需要的。使用''new''分配所有对象的方法和

在vector / deque中存储指向它们的指针,这是一个麻烦且容易发生内存泄漏错误的b $ b。此外,如果大多数对象与最大对象的大小大致相同,则将保存内存

与传统对象相比。办法。然而,这将允许在同一个双端队列中存储不同类型的物品。


我想知道这是否真的有用,或者是否有'' sa gotcha I

看不到。

I''m sure this is not a new idea, but I have never heard about it
before. I''m wondering if this could work:

Assume that you have a common base class and a bunch of classes
derived from it, and you want to make a deque which can contain any
objects of any of those types. Normally what you would have to do is to
make a deque or vector of pointers of the base class type and then
allocate each object dynamically with ''new'' and store the pointers in
the deque/vector. This is a hassle, memoryleak-prone, and in many cases
wastes memory compared to the situation where you have a deque
containing objects of one single type (especially if all the different
objects you want to store are approximately equal in size).

How about this solution:

A special deque which can contain different types of objects in the
same way as std::deque stores objects of the same type, but in this case
it would be possible to store objects of different types, even if the
objects have different sizes.

The only extra requirement would be that you have to tell the deque
the size of the largest object you are going to store in it.

So you instantiate this special deque by giving it the base class type
(which will be used to return references/pointers to the elements,
similarly to how a deque/vector of base class type pointers would work)
and the size of the largest object you will be storing in it (which can
also be a template parameter). I''m pretty sure that with template
metaprogramming it may even be possible to resolve the size of the
largest class by simply listing all the classes you are going to use in
some kind of template construct.

This deque will then allocate space for the elements in the same way
as std::deque does, but allocating space for each element with the given
maximum object size (instead of the size of the base class). The
insertion functions will be template functions themselves and will use
placement new to put the objects in their places in the deque. Since a
deque never needs to move objects around after they have been created,
self-assignment (with its problem related to the deque only knowing the
base class) will not be needed.

When this deque is accessed it will return references of the base
class type. The user can then do with them whatever is necessary (in the
same way as he would have to do with a vector of base class pointers).

Not all methods of std::deque can be offered, obviously (for example
erase() would be rather impossible to implement, so it''s not offered at
all) but in many cases that shouldn''t matter.

The largest advantage of this method would be that you don''t need to
worry about memory management, as you would need if you used the
"traditional" method of allocating all the objects with ''new'' and
storing pointers to them in the vector/deque, which is a hassle and
prone to memory leak errors. Also if most of the objects are
approximately the same size as the largest object, memory will be saved
compared to the "traditional" way. Yet this would allow storing objects
of different types inside the same deque.

I''m wondering if this could actually work, or if there''s a gotcha I
can''t see.

推荐答案

9月14日下午5:29,Juha Nieminen< ; nos ... @ thanks.invalidwrote:
On Sep 14, 5:29 pm, Juha Nieminen <nos...@thanks.invalidwrote:

我确定这不是一个新主意,但我从未听说过它

之前。我想知道这是否可行:


假设你有一个共同的基类和一堆类

派生自它,你想要的制作一个可以包含任何类型的任何

对象的双端队列。通常你需要做的是

制作一个deque或基类类型的指针向量然后

用''new''动态分配每个对象将指针存储在

deque / vector中。这是一个麻烦,记忆漏洞,并且在很多情况下,与你有一个deque

包含一种类型的对象的情况相比,内存浪费了内存(特别是如果全部的话)您要存储的不同

对象的大小大致相等。


这个解决方案怎么样:


一个特殊的双端队列,可以在

中包含不同类型的对象,与std :: deque一样,可以存储相同类型的对象,但在这种情况下

它将是即使

对象的大小不同,也可以存储不同类型的对象。


唯一的额外要求是你必须告诉deque

将要存储在其中的最大对象的大小。


因此,通过为其提供基类类型来实例化此特殊双端队列

(将用于返回元素的引用/指针,

类似于基类的deque / vector) s类型指针可以工作)

和你将存储在其中的最大对象的大小(可以

也是模板参数)。我很确定使用模板

元编程甚至可以通过简单地列出你要使用的所有类来解决

最大类的大小在

某种模板构造。


这个双端队列将以相同的方式为元素分配空间

as std :: deque,但是为每个元素分配空间,给定

最大对象大小(而不是基类的大小)。

插入函数本身就是模板函数,并将使用

placement new将对象放在双端队列中的位置。由于

deque在创建之后永远不需要移动物体,

自我分配(其问题与deque有关只知道
$不需要b $ b基类。


当访问此双端队列时,它将返回基本

类类型的引用。然后用户可以使用它们做任何必要的事情(在

中,与使用基类指针向量的方式相同)。


并不是所有的std :: deque方法都可以提供,显然(例如

erase()是不可能实现的,因此它不会在

中提供所有)但在很多情况下都不重要。


这种方法的最大优点是你不需要担心
担心关于内存管理,如果你使用

传统,你需要的。使用''new''分配所有对象的方法和

在vector / deque中存储指向它们的指针,这是一个麻烦且容易发生内存泄漏错误的b $ b。此外,如果大多数对象与最大对象的大小大致相同,则将保存内存

与传统对象相比。办法。然而,这将允许在同一个双端队列中存储不同类型的物品。


我想知道这是否真的有用,或者是否有''我知道我不能看到

I''m sure this is not a new idea, but I have never heard about it
before. I''m wondering if this could work:

Assume that you have a common base class and a bunch of classes
derived from it, and you want to make a deque which can contain any
objects of any of those types. Normally what you would have to do is to
make a deque or vector of pointers of the base class type and then
allocate each object dynamically with ''new'' and store the pointers in
the deque/vector. This is a hassle, memoryleak-prone, and in many cases
wastes memory compared to the situation where you have a deque
containing objects of one single type (especially if all the different
objects you want to store are approximately equal in size).

How about this solution:

A special deque which can contain different types of objects in the
same way as std::deque stores objects of the same type, but in this case
it would be possible to store objects of different types, even if the
objects have different sizes.

The only extra requirement would be that you have to tell the deque
the size of the largest object you are going to store in it.

So you instantiate this special deque by giving it the base class type
(which will be used to return references/pointers to the elements,
similarly to how a deque/vector of base class type pointers would work)
and the size of the largest object you will be storing in it (which can
also be a template parameter). I''m pretty sure that with template
metaprogramming it may even be possible to resolve the size of the
largest class by simply listing all the classes you are going to use in
some kind of template construct.

This deque will then allocate space for the elements in the same way
as std::deque does, but allocating space for each element with the given
maximum object size (instead of the size of the base class). The
insertion functions will be template functions themselves and will use
placement new to put the objects in their places in the deque. Since a
deque never needs to move objects around after they have been created,
self-assignment (with its problem related to the deque only knowing the
base class) will not be needed.

When this deque is accessed it will return references of the base
class type. The user can then do with them whatever is necessary (in the
same way as he would have to do with a vector of base class pointers).

Not all methods of std::deque can be offered, obviously (for example
erase() would be rather impossible to implement, so it''s not offered at
all) but in many cases that shouldn''t matter.

The largest advantage of this method would be that you don''t need to
worry about memory management, as you would need if you used the
"traditional" method of allocating all the objects with ''new'' and
storing pointers to them in the vector/deque, which is a hassle and
prone to memory leak errors. Also if most of the objects are
approximately the same size as the largest object, memory will be saved
compared to the "traditional" way. Yet this would allow storing objects
of different types inside the same deque.

I''m wondering if this could actually work, or if there''s a gotcha I
can''t see.



使用你最喜欢的智能指针的容器(例如提升)和你

不会

有内存泄漏。保持指向容器中基础对象的指针是一个常见的C ++习语。
a

常见的C ++习语。除非有一个

引人注目的

其他原因,否则我会坚持使用。

Use a container of your favorite smart pointer (e.g boost) and you
won''t
have memory leaks. Keeping pointers to base objects in containers is
a
common C++ idiom. I''d stick with what works unless there is a
compelling
reason to do otherwise.


Juha Nieminen写道:
Juha Nieminen wrote:

我敢肯定这不是一个新主意,但我以前从未听说过它。

之前。我想知道这是否可行:


假设你有一个共同的基类和一堆类

派生自它,你想要的制作一个可以包含任何类型的任何

对象的双端队列。通常你需要做的是

制作一个deque或基类类型的指针向量然后

用''new''动态分配每个对象将指针存储在

deque / vector中。这是一个麻烦,记忆漏洞,并且在很多情况下,与你有一个deque

包含一种类型的对象的情况相比,内存浪费了内存(特别是如果全部的话)您要存储的不同

对象的大小大致相同)。
I''m sure this is not a new idea, but I have never heard about it
before. I''m wondering if this could work:

Assume that you have a common base class and a bunch of classes
derived from it, and you want to make a deque which can contain any
objects of any of those types. Normally what you would have to do is to
make a deque or vector of pointers of the base class type and then
allocate each object dynamically with ''new'' and store the pointers in
the deque/vector. This is a hassle, memoryleak-prone, and in many cases
wastes memory compared to the situation where you have a deque
containing objects of one single type (especially if all the different
objects you want to store are approximately equal in size).



我通常使用一个容器< copy_ptr< T,或者我实现了一个类

Polymorphic_T,它可以保存T类型的值,并从T派生。然后,我

使用容器<我的程序中有Polymorphic_T和Polymorphic_T。

I usually use a container< copy_ptr<T, or I implement a class
Polymorphic_T that can hold values of type T and derived from T. Then, I
use container< Polymorphic_T and Polymorphic_T throughout my program.


这个解决方案怎么样:


一个特殊的deque,可以包含

中不同类型的对象与std :: deque存储相同类型的对象相同,但在这种情况下

可以存储不同类型的对象,即使

对象有不同的尺寸。


唯一的额外要求是你必须告诉deque

你将要存储在其中的最大对象的大小。


所以你通过给它基类类型来实例化这个特殊的双端类型

(这将是用于返回元素的引用/指针,

类似于基类类型指针的deque / vector如何工作)

和最大对象的大小你将存储在其中(可以

也是模板参数)。我很确定使用模板

元编程甚至可以通过简单地列出你要使用的所有类来解决

最大类的大小在

某种模板构造。


这个双端队列将以相同的方式为元素分配空间

as std :: deque,但是为每个元素分配空间,给定

最大对象大小(而不是基类的大小)。

插入函数本身就是模板函数,并将使用

placement new将对象放在双端队列中的位置。由于

deque在创建之后永远不需要移动物体,

自我分配(其问题与deque有关只知道
$不需要b $ b基类。


当访问此双端队列时,它将返回基本

类类型的引用。然后用户可以使用它们做任何必要的事情(在

中,与使用基类指针向量的方式相同)。


并不是所有的std :: deque方法都可以提供,显然(例如

erase()是不可能实现的,因此它不会在

中提供所有)但在许多情况下并不重要。
How about this solution:

A special deque which can contain different types of objects in the
same way as std::deque stores objects of the same type, but in this case
it would be possible to store objects of different types, even if the
objects have different sizes.

The only extra requirement would be that you have to tell the deque
the size of the largest object you are going to store in it.

So you instantiate this special deque by giving it the base class type
(which will be used to return references/pointers to the elements,
similarly to how a deque/vector of base class type pointers would work)
and the size of the largest object you will be storing in it (which can
also be a template parameter). I''m pretty sure that with template
metaprogramming it may even be possible to resolve the size of the
largest class by simply listing all the classes you are going to use in
some kind of template construct.

This deque will then allocate space for the elements in the same way
as std::deque does, but allocating space for each element with the given
maximum object size (instead of the size of the base class). The
insertion functions will be template functions themselves and will use
placement new to put the objects in their places in the deque. Since a
deque never needs to move objects around after they have been created,
self-assignment (with its problem related to the deque only knowing the
base class) will not be needed.

When this deque is accessed it will return references of the base
class type. The user can then do with them whatever is necessary (in the
same way as he would have to do with a vector of base class pointers).

Not all methods of std::deque can be offered, obviously (for example
erase() would be rather impossible to implement, so it''s not offered at
all) but in many cases that shouldn''t matter.



为什么?

Why?


这种方法的最大优点是你不需要

担心内存管理,如果您使用

传统,则需要这样做。使用''new''分配所有对象的方法和

在vector / deque中存储指向它们的指针,这是一个麻烦且容易发生内存泄漏错误的b $ b。此外,如果大多数对象与最大对象的大小大致相同,则将保存内存

与传统对象相比。办法。然而,这将允许在同一个双端队列中存储不同类型的物品。


我想知道这是否真的有用,或者是否有''我知道我不能看到

The largest advantage of this method would be that you don''t need to
worry about memory management, as you would need if you used the
"traditional" method of allocating all the objects with ''new'' and
storing pointers to them in the vector/deque, which is a hassle and
prone to memory leak errors. Also if most of the objects are
approximately the same size as the largest object, memory will be saved
compared to the "traditional" way. Yet this would allow storing objects
of different types inside the same deque.

I''m wondering if this could actually work, or if there''s a gotcha I
can''t see.



嗯,你首先可能想先尝试实现最简单的
容器:一个最多可容纳一个元素的盒子。已经在那里,你运行

陷入困境。考虑


struct BaseA {some stuff};

struct BaseB {some stuff};


struct D1: public BaseA,public BaseB {some more};

struct D2:public BaseB,public BaseA {some more};


什么是box< BaseA要去做。在某些元素中,BaseA子对象

在其他一些元素中排在第一位,排在第二位。因此,对于每个元素,您可以存储用于访问BaseA子对象的方法。每次取消引用条目时,都会调用该方法。

最佳


Kai-Uwe Bux

Well, you first might want to try first to implement the most simple
container: a box that can hold at most one element. Already there, you run
into trouble. Consider

struct BaseA { some stuff };
struct BaseB { some stuff };

struct D1 : public BaseA, public BaseB { some more };
struct D2 : public BaseB, public BaseA { some more };

What is box< BaseA going to do. In some elements, the BaseA subobject
comes first in some others, it comes second. So, with each element, you
could store a method for accessing the BaseA subobject. That method would
be called each time you dereference the entry.
Best

Kai-Uwe Bux


" Juha Nieminen" < no **** @ thanks.invalidwrote in message

news:46 ********************** @ news.song .fi ...
"Juha Nieminen" <no****@thanks.invalidwrote in message
news:46**********************@news.song.fi...

我确定这不是一个新主意,但我以前从未听说过它。

之前。我想知道这是否可行:


假设你有一个共同的基类和一堆类

派生自它,你想要的制作一个可以包含任何类型的任何

对象的双端队列。通常你需要做的是

制作一个deque或基类类型的指针向量然后

用''new''动态分配每个对象将指针存储在

deque / vector中。这是一个麻烦,记忆漏洞,并且在很多情况下,与你有一个deque

包含一种类型的对象的情况相比,内存浪费了内存(特别是如果全部的话)您要存储的不同

对象的大小大致相等。


这个解决方案怎么样:


一个特殊的双端队列,可以在

中包含不同类型的对象,与std :: deque一样,可以存储相同类型的对象,但在这种情况下

它将是即使

对象的大小不同,也可以存储不同类型的对象。


唯一的额外要求是你必须告诉deque

将要存储在其中的最大对象的大小。


因此,通过为其提供基类类型来实例化此特殊双端队列

(将用于返回元素的引用/指针,

类似于基类类型的deque / vector pe指针可以工作)

和你将存储在其中的最大对象的大小(可以

也是模板参数)。我很确定使用模板

元编程甚至可以通过简单地列出你要使用的所有类来解决

最大类的大小在

某种模板构造。


这个双端队列将以相同的方式为元素分配空间

as std :: deque,但是为每个元素分配空间,给定

最大对象大小(而不是基类的大小)。

插入函数本身就是模板函数,并将使用

placement new将对象放在双端队列中的位置。由于

deque在创建之后永远不需要移动物体,

自我分配(其问题与deque有关只知道
$不需要b $ b基类。


当访问此双端队列时,它将返回基本

类类型的引用。然后用户可以使用它们做任何必要的事情(在

中,与使用基类指针向量的方式相同)。


并不是所有的std :: deque方法都可以提供,显然(例如

erase()是不可能实现的,因此它不会在

中提供所有)但在很多情况下都不重要。


这种方法的最大优点是你不需要担心
担心关于内存管理,如果你使用

传统,你需要的。使用''new''分配所有对象的方法和

在vector / deque中存储指向它们的指针,这是一个麻烦且容易发生内存泄漏错误的b $ b。此外,如果大多数对象与最大对象的大小大致相同,则将保存内存

与传统对象相比。办法。然而,这将允许在同一个双端队列中存储不同类型的物品。


我想知道这是否真的有用,或者是否有''我知道我不能看到

I''m sure this is not a new idea, but I have never heard about it
before. I''m wondering if this could work:

Assume that you have a common base class and a bunch of classes
derived from it, and you want to make a deque which can contain any
objects of any of those types. Normally what you would have to do is to
make a deque or vector of pointers of the base class type and then
allocate each object dynamically with ''new'' and store the pointers in
the deque/vector. This is a hassle, memoryleak-prone, and in many cases
wastes memory compared to the situation where you have a deque
containing objects of one single type (especially if all the different
objects you want to store are approximately equal in size).

How about this solution:

A special deque which can contain different types of objects in the
same way as std::deque stores objects of the same type, but in this case
it would be possible to store objects of different types, even if the
objects have different sizes.

The only extra requirement would be that you have to tell the deque
the size of the largest object you are going to store in it.

So you instantiate this special deque by giving it the base class type
(which will be used to return references/pointers to the elements,
similarly to how a deque/vector of base class type pointers would work)
and the size of the largest object you will be storing in it (which can
also be a template parameter). I''m pretty sure that with template
metaprogramming it may even be possible to resolve the size of the
largest class by simply listing all the classes you are going to use in
some kind of template construct.

This deque will then allocate space for the elements in the same way
as std::deque does, but allocating space for each element with the given
maximum object size (instead of the size of the base class). The
insertion functions will be template functions themselves and will use
placement new to put the objects in their places in the deque. Since a
deque never needs to move objects around after they have been created,
self-assignment (with its problem related to the deque only knowing the
base class) will not be needed.

When this deque is accessed it will return references of the base
class type. The user can then do with them whatever is necessary (in the
same way as he would have to do with a vector of base class pointers).

Not all methods of std::deque can be offered, obviously (for example
erase() would be rather impossible to implement, so it''s not offered at
all) but in many cases that shouldn''t matter.

The largest advantage of this method would be that you don''t need to
worry about memory management, as you would need if you used the
"traditional" method of allocating all the objects with ''new'' and
storing pointers to them in the vector/deque, which is a hassle and
prone to memory leak errors. Also if most of the objects are
approximately the same size as the largest object, memory will be saved
compared to the "traditional" way. Yet this would allow storing objects
of different types inside the same deque.

I''m wondering if this could actually work, or if there''s a gotcha I
can''t see.



我知道boost有一个叫any_typeor anytype的东西。也许

适合你的目的。

I understand that boost has something called any_typeor anytype. Perhaps
that would suit your purpose.


这篇关于包含不同类型对象的双端队列(具有公共基类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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