问题描述
// 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
- 如何使
HVector
能够像普通std::vector
一样被初始化? - 我可以将
myVariant
放在HVElem
内并在构造类时更改其值吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)