在数组中保留模板类对象
Holding Template Class Objects in Array
我必须在一个数组中为我的项目保存不同类型的数据。我创建了一个用于生成对象的模板类。
template<class Queue>
class Template {
public:
Queue value;
Template(Queue input) {
value = input;
}
};
但如果不使用抽象类,我就无法将它们保存在一个数组中。我已经为此创建了一个空指针数组。我喜欢用它;
void *array[21];
array[index] = new Template<int>(number);
array[index] = new Template<string>(text);
没有抽象类,有什么可能的解决方案吗?我的意思是,我可以在模板类数组中保存这个模板对象吗?
创建层次结构并利用动态绑定:
class Base {
public:
virtual ~Base() {};
// ...
};
template<class Queue>
class Template : public Base {
Queue value;
public:
Template(Queue const &input) :value(input) {}
// ...
};
并将其用作:
Base *array[21];
array[index] = new Template<int>(number);
array[index + 1] = new Template<string>(text);
此外,不使用原始数组和原始指针,而是使用STL设施,如std::array
智能指针(例如,std::shared_ptr<Base>
或std::unique_ptr<Base>
):
std::array<std::unique_ptr<Base>, 21> arr;
arr[index].reset(new Template<int>(number));
arr[index + 1].reset(new Template<string>(text));
也更喜欢将成员变量初始化到构造函数的初始值设定项列表,而不是其主体。
这通常是一个迹象,表明您应该在更高的级别重新思考代码结构,这样您就根本不需要它了。
否则,我看到你有四个选择(如果你算上上面的"不要这样做",就有五个):
-
使用可以容纳所有类型数据的统一类型(例如
std::string
,并在需要时解析数字信息)。该功能可以封装在一个类中,该类提供成员函数以使其更容易实现。 -
使用
boost::variant
,如果你是C++的新手,那么我不建议你马上处理这类事情。 -
使用101010中解释的基类。我想补充一点,你可能需要在基类中有一个enum,它告诉你存储了什么类型的数据
-
使用
boost::any
,这比变体更难使用,尽管它更容易理解。
如果没有更多关于你想要实现的目标的信息,我们就无法为如何继续提供更好的指导。
我会使用变体类型来完成。您可以推出自己的产品,但更喜欢使用boost::variant
:
#include <boost/variant.hpp>
#include <iostream>
#include <string>
using namespace std;
template<class Queue>
class Template
{
public:
Queue value;
Template() = default;
Template(Queue input) {
value = input;
}
};
template<typename T>
std::ostream& operator<<(std::ostream& os, Template<T> const& t)
{
os << t.value;
return os;
}
int main ()
{
using v_t = boost::variant<Template<int>, Template<string>>;
v_t ar[2];
ar[0] = Template<int>(1);
ar[1] = Template<string>("lmfao");
for (auto&& elem : ar) cout << elem << endl;
}
演示
注意
v_t
是变体的类型v_t
可以有更多类型,您可以从中选择填充数组- 输出运算符只是为了演示而过载
- 您可以通过
boost::visitor
获得额外功能
相关文章:
- 保留计时器集合(对象与指针)的最佳方法
- 更改保留指向其字段的原始指针的对象地址
- 保留对象成员变量的本地副本
- 如何为同一类对象的成员函数保留单独的变量副本?
- 有没有办法在对象上保留弱引用并控制其生存期
- 在C 中进行可选参考,保留对象寿命
- C++对象保留?
- 是否应将移出对象保留为"safe"状态?
- 返回类型,或如何保留对象指针的类型
- 如何获得一个窗口,以便在调整C++大小时保留绘制的对象
- 实例变量列表中的对象未被保留?指针问题
- 保留一个reference_wrapper对象的向量,这是可能的
- 每次创建一个新对象或保留一个对象
- 如何在堆栈上为非默认构造对象保留空间
- 将临时对象保留在寄存器上以避免虚拟机中的额外存储/加载
- std::vector 不保留在实现中实例化的推回对象
- 如何保留指向插入到集合中然后推送到向量中的对象的指针
- 如何在不复制和保留 std::string 对象的情况下获得 C++ std::string 字符数据的所有权
- C++对象中保留 lambda 表达式
- 在c++ 11中,如何调用new并为对象保留足够的内存?