How come the following code snippet is compiled with no error:
void func(){ const int s_max{ 10 }; int m_array[s_max]{0}; } int main() { const int s_max{ 10 }; int m_array[s_max]{0}; return 0; } but when I try to define the same array within a class scope, I get the following error:
class MyClass { const int s_max{ 10 }; int m_array[s_max]{0}; // error: invalid use of non-static data member 's_max' }; Why does s_max need to be static within the class?
I could not find a convincing answer to my question in other similar posts.
constis necessary but not sufficient for a constant expression.s_maxis a constant expression. has integral or enumeration type and refers to a complete non-volatile const object, which is initialized with a constant expression