【发布时间】:2011-10-05 14:33:01
【问题描述】:
可能重复:
Is it ok to cast a STL container with Base type to Derived type?
这应该是一个快速的问题...如果我有一个容器,比如一个基类的 STL 列表,是否可以将整个容器转换为一种子类?例如
[A inherits from base class B]
list<B*> list1;
list1.push_back(new A());
list<A*> list2 = list1;
编译器在抱怨:“conversion from std::vector<B*, std:allocator<B*> >' to non-scalar type 'std::vector<Bar*, ...>' requested.”
这可能吗,还是我搞砸了?
【问题讨论】:
-
这是同一个问题,我相信:stackoverflow.com/questions/5460371/…
-
是的,你是对的。在提交之前我没有看到那个帖子...