1

I have never seen this:

class myclass{
    static int value;
};
int myclass::value(5);

This is a short version of a code that i see in the book C++ Concurrency in Action, but i don't get what is that declaration of a static class value out of the class block like that.

  • 1
    Does this answer your question? [Undefined reference to static class member](https://stackoverflow.com/questions/272900/undefined-reference-to-static-class-member) – François Andrieux Jun 20 '20 at 12:28
  • Not completly, in that qustion i can see that is a declaration of the value, but why is a 5 betweeen parenthesis? – Franz Kafka Jun 20 '20 at 12:30
  • So, the "5" would be the static initialization of the value + declaration? – Franz Kafka Jun 20 '20 at 12:31
  • `static int value;` is the declaration. `int myclass::value(5);` is the definition. The definition is equivalent to `int myclass::value = 5;`. – François Andrieux Jun 20 '20 at 12:32
  • This line initializes the static member. I think u r using the old edition before c++17 because with c++ 17. you can initialize the static members inside the definition of the class, as follows class myclass{ inline static int value {5}; }; – asmmo Jun 20 '20 at 12:34

1 Answers1

0
  1. :: is known as scope resolution operator and one of the purpose of it is to access a class’s static variables outside class and it seems above piece of code is doing same initializing it outside class.
  2. One of the ways to initialize variables in c++, known as constructor initialization, is done by enclosing the initial value between parentheses (()): So int myclass::value(5); is equivalent to int myclass::value = 5;