Search code examples
c++templatesinheritancestlvirtual

How can I manage inheritance in this case?


I'm trying to create a table in C++ that can contain various data types. Therefore I've created a virtual template class "Column" and derived from it "IntColumn", "FloatColumn", ecc. Then I wanted to create a std::vector to fill it with all the columns. The problem is that if I try to create a vector of Column type I should then specify the type of the Column (because it's a template) and if I want to create a Child type object I can't because it says the constructor is implicitly deleted.

I know it may seem stupid to you, but I'm new to these things and I can't really figure out what to do in this case.

My goal is to be able to create a vector of columns (that can store int, float, char, string, Date, Time types) and have methods that can extract data from all the types of columns. (Date and Time are classes I've already created)

If I should include some code just tell me.


Solution

  • I don't know what your column type should represent, so there are two approaches I'll give you:

    1. Describe a column. In that case, you have an array of column descriptors that describe a table and all you need is a "polymorphic container". Using that keyword should get you further.
    2. Contain values of a column. In that case, you're doing it the wrong way around. On the top level, use a container like vector to contain the rows (not columns!). Each row then consists of a simple struct or value class.