模板类中的C ++静态const数组初始化 [英] C++ static const array initialization in template class

查看:127
本文介绍了模板类中的C ++静态const数组初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模板类:

template <unsigned N>
class XArray {
  static const int Xdata[N];
};

我想为我使用的每个XArray<N>初始化静态const数组,例如,让XArray<N>::Xdata = {1, 2, 3, ..., N}.怎么做?

I want to initialize the static const array for each XArray<N> I used, for example, let XArray<N>::Xdata = {1, 2, 3, ..., N}. How to make it?

推荐答案

您在类中声明了一个静态const int数组,因此必须在类声明中定义静态成员,如下所示:

you declared a static const int array in your class,so you must define the static member out of the class declaration,just like this:

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};

但是,您必须注意的一点是:使用此模板时,必须确保"N"大于初始化数组的编号;

But something you must pay attention to is that: when you use this template you must make sure that the "N" bigger than the number of your initialized array;

这篇关于模板类中的C ++静态const数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆