在STL容器中切片实例 [英] Slicing instances in STL containers

查看:165
本文介绍了在STL容器中切片实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我有问题,当我使用std :: list< MyClassand然后在该列表中存储

MyClass的各个子类(或任何其他STL容器)

实例被切片。


我读过FAQ:''[20.8]什么是虚拟构造函数?''' ,但是如果有一些解决方法,我会错过信息。当使用STL

容器时,因为我不能改变它们来调用clone()而不是复制

构造函数。


我能想到的解决方案是:

a)在容器中存储指向MyClass的指针

- 为此我必须编写自定义复制ctr'和operator =( )

使用std :: list< MyClass>

b的所有类创建拥有的包装类我的班级

- 这个包装器会调用clone()来复制拥有的实例

- 然而这又是一个间接层,我将不得不

为几乎所有MyClass方法添加代理到这个包装器(我使用
大量使用STL算法来处理这个列表


还有其他吗?这个问题的简单解决方案,我可能会错过吗?


Ales

Hello,

I have problem that when I use std::list<MyClassand then store
various subclasses of MyClass in that list (or any other STL container)
the instances get sliced.

I have read FAQ: ''[20.8] What is a "virtual constructor"?'', but I
miss information if there is some "workaround" when using STL
containers, since I can''t change them to call clone() instead of copy
constructor.

The solutions I can think about is:
a) Store pointers to MyClass in the container
- for this I have to write custom copy ctr''s and operator=() for
all classes using std::list<MyClass>
b) Create wrapper class that "owns" my class
- this wrapper would call clone() to copy the owned instance
- this however one more level of indirection and I will have to
add proxy for almost all methods of MyClass to this wrapper (I make
heavy use of STL algorithms to work with the list

Is there any other "easy" solution to this problem, that I might miss?

Ales

推荐答案

AlesD写道:
AlesD wrote:

您好,


我有问题,当我使用std :: list< MyClassand然后存储

该列表中的MyClass的各个子类(或任何其他STL容器)

实例被切片。


我已阅读常见问题解答:'' [20.8]什么是虚拟构造函数?'',但是如果在使用STL

容器时有一些解决方法,我会错过信息
,因为我不能改变它们来打电话clone()而不是copy

构造函数。


我能想到的解决方案是:

a)存储指向MyClass的指针容器

- 为此我必须使用std :: list< MyClass>

所有类>
b)创建拥有的包装类我的班级

- 这个包装器会调用clone()来复制拥有的实例

- 然而这又是一个间接层,我将不得不

为几乎所有MyClass方法添加代理到这个包装器(我使用
大量使用STL算法来处理这个列表


还有其他吗?我可能会错过这个问题的简单解决方案吗?
Hello,

I have problem that when I use std::list<MyClassand then store
various subclasses of MyClass in that list (or any other STL container)
the instances get sliced.

I have read FAQ: ''[20.8] What is a "virtual constructor"?'', but I
miss information if there is some "workaround" when using STL
containers, since I can''t change them to call clone() instead of copy
constructor.

The solutions I can think about is:
a) Store pointers to MyClass in the container
- for this I have to write custom copy ctr''s and operator=() for
all classes using std::list<MyClass>
b) Create wrapper class that "owns" my class
- this wrapper would call clone() to copy the owned instance
- this however one more level of indirection and I will have to
add proxy for almost all methods of MyClass to this wrapper (I make
heavy use of STL algorithms to work with the list

Is there any other "easy" solution to this problem, that I might miss?



但是,使用智能指针可以让你的生活更轻松。

如果你想保留复制语义,你应该使用一个克隆智能指针

或一个复制智能指针。谷歌这个组的那些条款你将

如果你对引用计数基本引用语义感到满意,那么你可以使用tr1 :: shared_ptr。在任何一种情况下,编译器提供了副本

构造函数和赋值运算符对于包含


std :: my_list< well_chosen_smart_pointer< MyClass的类


会正常工作。

最好


Kai-Uwe Bux

Pointers it is, however, you can make your life easier using smart pointers.
If you want to keep copy semantics, you should use a cloning smart pointer
or a copy smart pointer. Google this group for those terms and you will
find code. If you are happy with reference count base reference semantics,
you can use tr1::shared_ptr. In either case, the compiler provided copy
constructors and assignment operators for classes that contain a

std::my_list< well_chosen_smart_pointer< MyClass

will work fine.
Best

Kai-Uwe Bux


2006年8月28日星期一07:36:13 -0400,Kai-Uwe Bux< jk ******** @ gmx.net>

写道:
On Mon, 28 Aug 2006 07:36:13 -0400, Kai-Uwe Bux <jk********@gmx.net>
wrote:

>但是,使用智能指针可以让你的生活更轻松。
如果你想保留复制语义,你应该使用克隆智能指针
或复制智能指针。谷歌这一组用于那些条款,你将找到代码。如果您对引用计数基本引用语义感到满意,那么您可以使用tr1 :: shared_ptr。在任何一种情况下,编译器都为包含


std :: my_list<的类提供了复制
构造函数和赋值运算符。 well_chosen_smart_pointer< MyClass

会正常工作。
>Pointers it is, however, you can make your life easier using smart pointers.
If you want to keep copy semantics, you should use a cloning smart pointer
or a copy smart pointer. Google this group for those terms and you will
find code. If you are happy with reference count base reference semantics,
you can use tr1::shared_ptr. In either case, the compiler provided copy
constructors and assignment operators for classes that contain a

std::my_list< well_chosen_smart_pointer< MyClass

will work fine.



你能解释哪些方法可行吗?当然,tr1 :: shared_ptr给你

对象生命周期管理,虽然目前为止效率最低的是
(每个指向对象一个动态分配的计数器)。 />
否则,tr1 :: shared_ptr模仿真实的指针,因此

''继承''STL值语义和指针之间的不匹配。所以,

为什么要把聪明指向STL容器的指针?没有

感。


祝福,

Roland Pibinger

Can you explain what will work fine? Sure, tr1::shared_ptr gives you
object lifetime management, albeit the most inefficient known so far
(one dynamically allocted counter for each pointed-to object).
Otherwise, tr1::shared_ptr imitates real pointers and therefore
''inherits'' the mismatch between STL value semantics and pointers. So,
why put "smart" pointers into STL containers at all? It makes no
sense.

Best wishes,
Roland Pibinger


Roland Pibinger写道:
Roland Pibinger wrote:

>会正常工作。
>will work fine.



[反容器指针咆哮编辑]

[anti-pointer-in-container rant redacted]



好​​的,罗兰。我有以下要求:我需要一个

多态对象的容器。出于各种原因,在这个项目上,我不允许使用第三方图书馆(不建议使用Rogue Wave)。


我该怎么办?做到了吗?

OK, Roland. I have the following requirements: I need a container of
polymorphic objects. For various reasons, on this project, I am not
allowed to use third party libraries (don''t suggest Rogue Wave).

How do I do it?


这篇关于在STL容器中切片实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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