[英]How to create a vector that saves elements of all types of the same template class
I have a template class: 我有一个模板类:
template<typename A, typename B, typename C>
class blah{...};
I want to create a std::vector<> to save any possible objects of blah. 我想创建一个std :: vector <>来保存任何可能的对象。
for example 例如
blah<float, int, string>
or 要么
blah<string, string, char>
those blah objects, although originated from the same template class, are essentially different types. 这些对象虽然起源于同一模板类,但实际上是不同的类型。 How can I declare a std::vector to save them all? 如何声明一个std :: vector来保存它们?
I thought about using std::variant, but I can't because to use it, I need to enumerate all possible template parameter combinations: 我考虑过使用std :: variant,但是我不能,因为要使用它,我需要枚举所有可能的模板参数组合:
std::variant<all possible template parameter combinations>
I thought about using std::any, I can't because I don't know how to type cast from std::any back to the right type? 我考虑过使用std :: any,因为我不知道如何键入从std :: any返回正确类型的类型?
I thought about using a base class: 我考虑过使用基类:
template<typename A, typename B, typename C>
class blah : public blah_base
{...};
and declare a vector like this: 并声明一个像这样的向量:
std::vector<blah_base*>
but again, I still don't know how to type cast from blah_base* back to the right type? 但是同样,我仍然不知道如何将blah_base *的类型转换回正确的类型?
Basically, what you need is polymorphism. 基本上,您需要的是多态性。 All of the options you were exploring are polymorphism, and that is the best way for this to be solved. 您正在探索的所有选项都是多态的,这是解决此问题的最佳方法。 However, you say this and other variations: 但是,您说这和其他变体:
I don't know how to type cast from blah_base* back to the right type 我不知道如何将blah_base *的类型转换回正确的类型
If you find yourself needing to do this, you are doing something wrong. 如果您发现自己需要这样做,那说明您做错了。 Either it doesn't make sense to have a std::vector<blah<...>>
, or your blahs all need to meet the blah_base contract. 要么拥有std::vector<blah<...>>
没有任何意义,要么所有的std::vector<blah<...>>
都需要满足blah_base合同。
You could solve this with a virtual function: 您可以使用虚函数解决此问题:
class blah_base {
public:
virtual void foo() = 0;
};
Then, rather than having to tell which type of blah
you had, you'd just call the appropriate foo
function that does what you want. 然后,您不必调用哪种类型的blah
,而只需调用执行所需功能的适当的foo
函数即可。
Well, without knowing too much about what your use case is I will assume you will have three vectors; 好吧,在不完全了解您的用例的情况下,我将假定您将拥有三个向量。 one for each template type. 每个模板类型一个。 You could implement blah
as such: 您可以这样实现blah
:
#include <vector>
template<typename A, typename B, typename C>
class blah
{
public:
// Put stuff here.
void AddToVectorA(A item);
void AddToVectorB(B item);
void AddToVectorC(C item);
private:
std::vector<A> _a;
std::vector<B> _b;
std::vector<C> _c;
};
template<typename A, typename B, typename C>
void blah<A,B,C>::AddToVectorA(A item)
{
_a.push_back(item);
}
template<typename A, typename B, typename C>
void blah<A,B,C>::AddToVectorB(B item)
{
_b.push_back(item);
}
template<typename A, typename B, typename C>
void blah<A,B,C>::AddToVectorC(C item)
{
_c.push_back(item);
}
int main()
{
blah<int, double, float> b;
b.AddToVectorA(2);
return 0;
}
You could also make the three vectors public and people can use them however; 您也可以将这三个向量公开,但是人们可以使用它们。 you could also look into tuple
. 您也可以查看tuple
。 FYI: I now tested the code, it works. 仅供参考:我现在测试了代码,它可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.