I was reading through static keyword usage.
I came across this explanation - "An important detail to keep in mind when debugging or implementing a program using a static class member is that you cannot initialize the static class member inside of the class. In fact, if you decide to put your code in a header file, you cannot even initialize the static variable inside of the header file; do it in a .cpp file instead. Moreover, you are required to initialize the static class member or it will not be in scope."
My question is, why can't we initialize static class member inside the class? Why should it always be outside the class?
@keskiverto: My question is why we cannot initialize static class member inside the class. Let me give an example for more clarity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* More stuff for the class user */
id = user::next_id++; //or, id = user.next_user_id();
int user::next_id = 0;
This was the code provided in cprogramming tutorials. My question is why can't I do staticint next_id = 0; inside class user? Why should it be outside the class?