[英]C++ : Vector of template class
我有一个名为Cell的模板类如下: -
template<class T>class Cell
{
string header, T data;
}
现在我想要另一个名为Row的类。 Row将有一个名为Cells的向量,以便我可以将Cell和Cell类型元素添加到该向量。 可能吗?
如果是这样,我该怎么做? 提前致谢。
通过您提供的额外细节,前两个答案将无效。 你需要的是一种被称为细胞变体的类型,然后你可以有一个矢量。 例如:-
enum CellType
{
Int,
Float,
// etc
};
class Cell
{
CellType type;
union
{
int i;
float f;
// etc
};
};
class Vector
{
vector <Cell> cells;
};
然而,添加新类型是一件痛苦的事情,因为它需要大量的代码来维护。 另一种方法是使用具有公共基类的单元格模板: -
class ICell
{
// list of cell methods
};
template <class T>
class Cell : public ICell
{
T data;
// implementation of cell methods
};
class Vector
{
vector <ICell *> cells;
};
这可能会更好,因为您最初需要更少的代码来更新以添加新的单元格类型,但您必须在单元格向量中使用指针类型。 如果按值存储单元格, vector <ICell>
,则由于对象切片而丢失数据。
这在C ++中是不可能的 ,但在Java / Python中可能是因为:在C ++向量中,STL容器的存储(由vector :: data()返回)包含所有顺序打包的对象实例。 其中每个元素必须具有相同的大小。 这使得寻址快速方便。 因此,假设您定义了模板类A,
template <class T>
class A{
int id;
T obj;
};
其大小取决于模板变量“T obj”。 推动不同模板类型T的相同类A将使向量中的每个元素具有不同的大小,因此,这是不可能的。 唯一的方法是使用基类的shared_ptr或unique_ptr的向量。 C ++ 11和Boost都支持shared_ptr和unique_ptr。 每个派生类元素可以具有不同的模板类型。 这样,当调用基类指针的析构函数时,将调用派生类的析构函数。 例如,
#include <memory>
#include <vector>
#include <iostream>
#include <string>
using namespace std;
class A{};
template <class T>
class AImpl : public A{
public:
T obj;
AImpl(T _obj):obj(_obj){}
~AImpl(){
cout << "Deleting " << obj << endl;
}
};
int main(int argc, char** argv)
{
AImpl <string>* a1 = new AImpl <string> ("string1234");
AImpl <int>* a2 = new AImpl <int> (1234);
AImpl <double>* a3 = new AImpl <double> (1.234);
vector <shared_ptr<A>> As;
As.push_back(shared_ptr<A>(a1));
As.push_back(shared_ptr<A>(a2));
As.push_back(shared_ptr<A>(a3));
}
请记住使用-std = c ++ 11进行编译以启用C ++ 11。
输出:
Deleting string1234
Deleting 1234
Deleting 1.234
而你得到你想要的! :)
在Java / Python中,每个类对象变量实际上都是一个指针,因此,A的Java数组或A的Python列表等同于A的C ++指针数组。因此,如果没有明确创建,您将获得基本相同的功能shared_ptrs。
像这样的东西?
template<class T>
class Row
{
private:
std::vector<Cell<T> > cells;
};
好的,这个答案是不正确的。
所以,如果你想在一个vector
存储不同的单元格 - 你应该使用一些动态类型识别(你可以使用一个基类并在向量中存储指向它的指针,它只使用虚拟函数,在所有派生类中都被覆盖,你可以存储类似boost::any
东西,并为每个插入元素保存一些type-identification
,以便将它们转换为实际类型并使用它。
另一个答案是好的,但你可能想要:
template<class T>
class Row
{
private:
class Cell {
string header;
T data;
}
std::vector<Cell> cells;
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.