C ++: template class vector

I have a template class called Cell as follows: -

template<class T>class Cell { string header, T data; } 

Now I need another Named Row class. The string will have a Cell vector such that I can add Cell and Cell type elements to this vector. Is it possible?

If so, how can I do this? Thanks in advance.

+8
c ++ generics vector
source share
4 answers

With the additional details you provided, the first two answers will not work. What you require is a type known as an option for a cell, and then you can have a vector. For example: -

 enum CellType { Int, Float, // etc }; class Cell { CellType type; union { int i; float f; // etc }; }; class Vector { vector <Cell> cells; }; 

It is, however, a pain to add new types, as this requires a lot of code. An alternative could be to use a cell template with a 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, since you have less source code to update to add a new cell type, but you must use the pointer type in the cell vector. If you save a cell by value, vector <ICell> , you will lose data due to cutting of objects .

+12
source share

Something like that?

 template<class T> class Row { private: std::vector<Cell<T> > cells; }; 

Well, this answer is incorrect.

So, if you want to save different cells in one vector - you should use some dynamic type identification (you can use one base class and store a pointer to it in a vector that uses only virtual functions that are redefined in all derived classes you can store what something like boost::any and save some type-identification for each inserted element, for their inclusion in the real type and work with it).

+6
source share

Another answer is good, but you probably wanted:

 template<class T> class Row { private: class Cell { string header; T data; } std::vector<Cell> cells; ... } 
+5
source share

The reason this is NOT possible in C ++, but possible in Java / Python, is because: in the C ++ vector, the STL container store (returned by vector :: data ()) contains the entire instance of the object sequentially packed. Each item must be the same size. This allows quick and convenient handling. Therefore, suppose you define a template class A,

 template <class T> class A{ int id; T obj; }; 

Its size will depend on the template variable "T obj". Pressing the same class A of another type of template T will make each element in the vector have different sizes, so this is not possible. The only way is to use the shared_ptr or unique_ptr vector of the base class. Both shared_ptr and unique_ptr are supported by C ++ 11 and Boost. Each element of a derived class can have different types of templates. Thus, when the base class pointer destructor is called, the derived class destructor is called. For example,

 #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)); } 

Remember to compile with -std = C ++ 11 to enable C ++ 11.

Output:

 Deleting string1234 Deleting 1234 Deleting 1.234 

And you get what you want! :)

In Java / Python, each object class variable is actually a pointer, so a Java A or Python array from A is equivalent to a C ++ array of pointers A. So you get essentially the same functionality without explicitly creating shared_ptrs.

+5
source share

All Articles