模板化继承对象的容器 [英] Templated containers of inherited objects

查看:77
本文介绍了模板化继承对象的容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想要你的帮助...


我们可以有一个std :: list< BASEwhere BASE是一个类

层次结构的基类?我想在这个列表中添加继承自

BASE类的对象,但不一定相同......


类库{

int x;

};


class child1:public base {

int d;

};


class child2:public base {

int e;

};


int main(){

std :: list< basemylist;

child1 _c1;

child2 _c2;

mylist.push_back(_c1);

mylist.push_back(_c2);

返回0;

}


似乎我丢失了_c1和_c2的信息,就好像它们是
转换为基数。


请,你知道我怎么能实现这个目标吗?我需要实现自己的容器吗?


非常感谢提前,

- George

Hi everyone,

I''d like your help...

Can we have a std::list<BASEwhere BASE be the base class of a class
hierarchy? I want to add to this list objects that are inherited from
BASE class but not necessarily the same...

class base {
int x;
};

class child1 : public base {
int d;
};

class child2 : public base {
int e;
};

int main() {
std::list<basemylist;
child1 _c1;
child2 _c2;
mylist.push_back(_c1);
mylist.push_back(_c2);
return 0;
}

It seems that I lose the information of _c1 and _c2 like they are
converted to base.

Please, do you have any idea how can I achieve that? Do I have to
implement my own container?

Many thanks in advance,
-- George

推荐答案

George Exarchakos写道:
George Exarchakos wrote:

>


我们可以有一个std :: list< BASEwhere BASE是一个类

层次结构的基类吗?我想在这个列表中添加继承自

BASE类的对象,但不一定相同...





拜托,您知道我怎么能实现这一目标?我是否必须实施我自己的容器?b
$ b
>

Can we have a std::list<BASEwhere BASE be the base class of a class
hierarchy? I want to add to this list objects that are inherited from
BASE class but not necessarily the same...

[redacted]

Please, do you have any idea how can I achieve that? Do I have to
implement my own container?



你遇到了切片问题。你需要为你的基类创建一个

(智能)指针的容器。


即std :: list< base *>,或者std :: list< some_smart_ptr< base


参见FAQ, http://www.parashift.com/c++-faq-lit....html#faq-34.4


You''ve run into the slicing problem. You need to create a container of
(smart) pointers to your base class.

That is, std::list<base*>, or std::list<some_smart_ptr<base

See the FAQ, http://www.parashift.com/c++-faq-lit....html#faq-34.4



2月16日下午5:51,red floyd< no.s ... @ here.dudewrote:
On Feb 16, 5:51 pm, red floyd <no.s...@here.dudewrote:

George Exarchakos写道:
George Exarchakos wrote:

我们可以有一个std :: list< BASEwhere BASE是一个类的基类

层次结构?我想在这个列表中添加继承自

BASE类的对象但不一定相同...
Can we have a std::list<BASEwhere BASE be the base class of a class
hierarchy? I want to add to this list objects that are inherited from
BASE class but not necessarily the same...




>
[redacted]
>


请问,您有什么想法我能做到这一点吗?我是否必须实施我自己的容器?b
$ b
Please, do you have any idea how can I achieve that? Do I have to
implement my own container?



你遇到了切片问题。你需要为你的基类创建一个

(智能)指针的容器。


即std :: list< base *>,或者std :: list< some_smart_ptr< base


参见FAQ, http://www.parashift.com/c++-faq-lit....html#faq-34.4



非常感谢Red Floyd!这个小贴士正是我想要的...

- George

Many thanks Red Floyd! This tip is exactly what I was looking for...
-- George


2007年2月16日09:17: 51-0800,George Exarchakos写道:
On 16 Feb 2007 09:17:51 -0800, "George Exarchakos" wrote:

>我们可以有一个std :: list< BASEwhere BASE是类
层次结构的基类吗?我想在这个列表中添加继承自BASE类但不一定相同的对象...
>Can we have a std::list<BASEwhere BASE be the base class of a class
hierarchy? I want to add to this list objects that are inherited from
BASE class but not necessarily the same...



STL不支持面向对象的编程。 STL容器,
迭代器和算法仅针对值设计,而不是针对

(指向)对象。存在变通方法但是使用''标准''STL

在使用对象时始终保持阻抗不匹配。


祝你好运,

Roland Pibinger

STL does not support object-oriented programming. STL containers,
iterators and algorithms are designed only for values, not for
(pointers to) objects. Workarounds exist but with ''Standard'' STL there
always remains an ''impedance mismatch'' when objects are used.

Best regards,
Roland Pibinger


这篇关于模板化继承对象的容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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