generics - C++ : Vector of template class -
i have template class named cell follows:-
template<class t>class cell { string header, t data; }
now want class named row. row have vector named cells such can add both cell , cell type elements vector. possible?
if so, how can that? in advance.
with detail you've provided, first 2 answers won't work. require type known variant cell , can have vector of those. example:-
enum celltype { int, float, // etc }; class cell { celltype type; union { int i; float f; // etc }; }; class vector { vector <cell> cells; };
this, however, pain add new types requires lot of code maintain. alternative use cell template common base class:-
class icell { // list of cell methods }; template <class t> class cell : public icell { t data; // implementation of cell methods }; class vector { vector <icell *> cells; };
this might work better have less code update add new cell type have use pointer type in cells vector. if stored cell value, vector <icell>
, lose data due object slicing.