Initializing Constant Static Array In Header File

后端 未结 6 1955
春和景丽
春和景丽 2020-12-13 12:31

I have just found out that the following is not valid.

//Header File
class test
{
    const static char array[] = { \'1\', \'2\', \'3\' };
};
6条回答
  •  有刺的猬
    2020-12-13 13:05

    You can always do the following:

    class test {
      static const char array(int index) {
        static const char a[] = {'1','2','3'};
        return a[index];
      } 
    };
    

    A couple nice things about this paradigm:

    • No need for a cpp file
    • You can do range checking if you want to
    • You avoid having to worry about the static initialization fiasco

提交回复
热议问题