0

What exactly is the difference between these two members in question of initialization and instantiation?:

class Test {
    // ctor, dtor, ... here
private:
    static int m_test = 0;             // error - non-const ...
    static const int m_const_test = 0; // working - const ...
};

Why is initialization of non-const members not valid? And when will both variables (assuming non-const member without init!) be instantiated?

Edit: Even if a good part of this question is superimposable to the mentioned posts, i don't think that every part of my question is answered in these entries.

BR, Tuna

ttuna
  • 111
  • 1
  • 9
  • 3
    possible dublicate http://stackoverflow.com/questions/9656941/why-i-cant-initialize-non-const-static-member-or-static-array-in-class – Yuriy Orlov Feb 24 '16 at 14:25

2 Answers2

1

If multiple compilation units include your class definition then in which one should be your static int m_test = 0 be placed? For const static ... it does not matter because it is const.

knivil
  • 787
  • 3
  • 11
0

m_const_test being declared as const can´t be changed.

So, you can save a little bit of time using m_const_test, but is not as flexible as m_test.