在单个列表中有多个类类型 [英] Multiple class types in a single list
问题描述
是否可以在单个 std :: list
对象中存储多个不同的类类型?
Is it possible to store multiple different class types in a single std::list
object?
我想创建一个多层列表,可以存储 int
和 std :: list
作为一个元素,现在我试图使用 std :: list
。我需要从顶层和后续图层的所有列表中检索数据。
I want to make multi-layered list that can store int
and std::list
as an element, and now am trying to use std::list
for this purpose. I need to retrieve data from all the list from both top-level and subsequent layers too.
This question here is similar except that I want to use pre-defined class.
以下代码在第4行返回错误:
The following returns error at 4th line:
std::list<int> l;
std::list<int> subL;
l.push_back(1);
l.push_back(subL);
这会在第1(和第1行的第3行)返回错误:
This returns error at 1st (and 3th according to the 1st line):
std::list l;
std::list<int> subL;
l.push_back(1);
l.push_back(subL);
推荐答案
您可以声明 list< ; void *>
并存储到指向任何对象的指针。但另一个问题将提高你如何区分他们使用?如果你不需要,可以。
You could declare list <void*>
and store to it pointers to any of your objects. But another question will raise how you're going distinguish them to use? If you don't need to, it's OK.
这篇关于在单个列表中有多个类类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!