Skip navigation

A bit of a weird issue, when you declare a static variable in VS 2015 you can initialize it in the member declaration:

// Unit.h
struct Unit
{
  static const char Character = 'P'; // class-level var
  char symbol; // member var
  Unit()
  {
    symbol = Character; // init with static var
  }
};

Something weird happens with the compiler sometimes however. On multiple compile/runs, the variable of the static const char Character, when changed, doesn’t bake properly into the exe.

To fix this, just split the declaration and initialization into a corresponding .cpp,

// Unit.h:
struct Unit
{
  static const char Character; // class-level var, init in .cpp
  char symbol; // member var
  Unit()
  {
    symbol = Character; // init with static var
  }
};

// Unit.cpp
const char Unit::Character = 'P';

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: