我有下面的类:C++店相同的类具有不同的模板阵列
template <typename T>
class A
{
public:
void method(const char *buffer);
// the template T is used inside this method for a local variable
};
现在我需要这个类中包含不同的模板实例组成的数组:
std::vector<A*> array;
array.push_back(new A<uint32_t>);
array.push_back(new A<int32_t>);
但std::vector<A*> array;
不会工作,因为我显然需要指定一个模板,但我不能这样做,因为我在这个数组中存储不同的类型。是否有某种通用类型或其他解决方案?
你能只是一个成员函数模板,而不是整个类? –
你可以让所有'A's继承自一个基地,虽然这往往是不好的。 – Pubby
除了Martinho所说的,'A'不是一种类型,它是一个类模板。所以你不能像'std :: vector '那样将它用作模板参数。 – mfontanini