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.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -