Is there a way to conditionally initialize a global static variable?

  c++, static-initialization

So my current code looks like the following:

static Item fields[] = 
{
{GROUP1, TEXT1},
{GROUP2, 0},
}

Now I need to make change in such a way that I initialize GROUP2 only if certain condition is met else need to initialize with GROUP3. So I tried the following:

static Item fields[] = (flagSet)?
{
{GROUP1, TEXT1},
{GROUP2, 0},
} : {
{GROUP1, TEXT1},
{GROUP3, 0},
}

But this didn’t work. I know that one way is to use #ifdef macros but this flagSet happens at runtime and based on that I need to initialize the static array. Also since the static initialization happens before anything else is it possible to do this at all?

Source: Windows Questions C++

LEAVE A COMMENT