我有一个C ++模板化类,其构造函数具有默认参数。
可以使用非默认arg实例化为数组吗? (如果没有,为什么不呢?)
任一种都能奏效,但不能同时奏效(在g ++ 4.6.3中):
template <class T> class Cfoo {
public:
int x;
Cfoo(int xarg=42) : x(xarg) {}
};
Cfoo<int> thisWorks[10];
Cfoo<int> thisWorks(43);
Cfoo<int> thisFails(43)[10];
Cfoo<int> thisFails[10](43);
Cfoo<int>[10] thisFails(43);
// (even crazier permutations omitted)
最佳答案
您是正确的:您只能在数组中默认构造元素,而您可以将任何您喜欢的参数传递给单个对象构造。
如果需要收集,在C ++ 98中可以使用std::vector
:
std::vector<Cfoo<int> >(10, 43);
关于c++ - 使用arg,*和*作为数组实例化C++模板化类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18854702/