Oli*_*r Q 5 c++ arrays static templates initialization
我有以下模板类:
template <unsigned N>
class XArray {
static const int Xdata[N];
};
Run Code Online (Sandbox Code Playgroud)
我想为每个XArray<N>我使用的静态常量数组初始化,例如, let XArray<N>::Xdata = {1, 2, 3, ..., N}。如何制作?
您在类中声明了一个 static const int 数组,因此您必须在类声明之外定义静态成员,就像这样:
template<unsigned N>
class XArray
{
public:
static const int array[N];
};
template<unsigned N>
const int XArray<N>::array[N] = {1,2,3,4,5};
Run Code Online (Sandbox Code Playgroud)
但必须注意的是:使用该模板时必须确保“N”大于初始化数组的个数;