【发布时间】:2015-11-03 19:43:46
【问题描述】:
我必须为我的项目在一个数组中保存不同类型的数据。我创建了一个用于生成对象的模板类。
template<class Queue>
class Template {
public:
Queue value;
Template(Queue input) {
value = input;
}
};
但我不能在不使用抽象类的情况下将它们保存在一个数组中。我为此创建了一个 void 指针数组。我喜欢它;
void *array[21];
array[index] = new Template<int>(number);
array[index] = new Template<string>(text);
没有抽象类有没有可能的解决方案?我的意思是,我可以在模板类的数组中保存这个模板对象吗?
【问题讨论】:
-
您可能误解了模板参数。
int实际上如何代表Queue? -
不,我将在队列实现中使用不同类型的数据。因为这个,我给它命名了。
-
您可能需要一些中间模板类,例如
template <class T> class Queue;,其中T分别实例化为int、std::string。 -
您介意我问您为什么删除了该代码 sn-p 吗?您正在删除其他读者理解人们答案的必要上下文。