如何将我创建的自定义容器放入库中,以便可以像其他任何容器一样进行初始化例如,如std :: 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();
};

在这代码中,我使用HVElemVar.hh声明存储在HVElem的每个HVector中的类型。问题是我可能需要在同一程序中使用多个HVector,它们具有不同的类型集。基本上,我希望能够做到这一点:

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. 如何使HVector能够像普通std::vector一样被初始化?
  2. 我可以将myVariant放在HVElem内并在构造类时更改其值吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)