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
- How can I make
HVector
able to be initialized like normalstd::vector
? - Can I put
myVariant
insideHVElem
and change it's value when class is contructed?