Search code examples
c++static-initializationstatic-initializerstatic-array

A better way to initialize a static array member of a class in C++ ( const would be preferred though )


I have a static array of pointers to functions as a member of a class.

I need to initialize it, but it turns out this array is 64K items long, so it's impractical to initialize it with a static initializer like { x, y, z, ... } as it would clutter code.

I have instead to initialize it by code, with several loops.

The way I figured to do this is by initializing the static array in the constructor and setting a flag to it, so only the construction of the first instance of the class would fire this initialization.

Also accessing this static flag from within instances would not be thread safe, but that's another story.

Is there a cleaner or better way of doing this?

I also would like this array to be const, but I'm afraid the only way to do it is with the static {} initialization, right?


Solution

  • Another option would be to use code generation: Write a separate program that generates the source code for the definition of the static array.