c++ - Const array initialization if its too long -


in answer question mentioned code posted wouldn't work if variable declared const.

what if want initialize long array that's const type

int main (){     const int ar[100];  //this invalid in c++  //something      (int i=0;i <100;++i)         ar[i]=i;     return 0; } 

are there other ways using #define method proposed in question?

there's no way directly, without hacks.

in c++ 1 way work around problem take advantage of fact inside constructors const objects still modifiable. so, can wrap array class , initialize in constructor

struct arr  {   int a[100];    a()    {     (unsigned = 0; < 100; ++i)       a[i] = i;   } }; 

and declare

const arr arr; 

of course, have access arr.a[i], unless override operators in arr.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -