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

1

2 Answers 2

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.

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.