C++ 如何放置自定义容器I';您是否已经创建到库中,以便可以像任何其他容器一样初始化它(例如std::vector)?
下面是我的容器的头文件代码,它是一个异构向量C++ 如何放置自定义容器I';您是否已经创建到库中,以便可以像任何其他容器一样初始化它(例如std::vector)?,c++,oop,file-structure,C++,Oop,File Structure,下面是我的容器的头文件代码,它是一个异构向量 // 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>
// 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();
};
HVector
能够像普通std::vector
一样初始化myVariant
放入HVElem
中并更改其值吗值得一看:您的
HVector
类没有模板化,因此不能像模板一样使用它。。。您只需执行HVector v2代码>。看来你会从阅读一本书中受益。您正在寻找模板。
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