0

Below is the code of header files for my container which is a heterogeneous vector.

// HVElemVar.hh 
#pragma once
#include <variant>
// contains headers of types that will be stored in the container
#include <Type1.hh>
#include <Type2.hh>    

using myVariant = std::variant<Type1, Type2>;


// HVElem.hh
#pragma once
#include <iostream>
#include <HVElemVar.hh>

class HVElem
{
public:
    HVElem( myVariant newData );
    ~HVElem();
    myVariant getData();
private:
    myVariant data;
};


// HVector.hh
#pragma once
#include <vector>
#include <variant>
#include <HVElem.hh>


class HVector: public std::vector<HVElem>
{
public:
    HVector();
    void push(myVariant newData);
    void show();
};

In this code, I use HVElemVar.hh to declare types stored inside each HVElem of HVector. The issue is that I might need multiple HVector's with a different set of types in the same program. Basically I want to be able to do this:

std::vector<Type1> v1;
HVector<Type1, Type2> v2; // just  like with normal std::vector  
HVector<Type3, Type4> v3; // another HVector but with different types inside
  1. How can I make HVector able to be initialized like normal std::vector?
  2. Can I put myVariant inside HVElem and change it's value when class is contructed?
trofchik
  • 87
  • 1
  • 7
  • Worth a look: https://stackoverflow.com/questions/4353203/thou-shalt-not-inherit-from-stdvector – Holt Sep 30 '20 at 09:46
  • 1
    Your `HVector` class is not templated, so you cannot use it like a template... You simply do `HVector v2;`. – Holt Sep 30 '20 at 09:47
  • 1
    It seems like you would benefit from reading a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). You are looking for templates. – Passer By Sep 30 '20 at 09:48

0 Answers0