Initialization of a variable of a static class inside the main

I have a static variable in the class. I initialize that on a global scale his work is beautiful.

But when I try to initialize in the main linker, an error occurs. Why is this so.

class Myclass{

    static int iCount;
} ;

int main(){

  int Myclass::iCount=1;

}

And In the global scope, I have to specify the type of the variable as

int Myclass::iCount=1;

As in my class, I define iCount as an integer type, why not.

   Myclass::iCount =1 ; in //Global scope
+5
source share
5 answers

The $ 9.4.2 / 7 section of C ++ Standard says:

Static data elements are initialized and destroyed just like non-local objects (3.6.2, 3.6.3).

" " . , , .

, , Myclass::iCount. , ( , ), :

class Myclass{

    static int iCount;
} ;
int Myclass::iCount=1;

int main(){
  /*** use Myclass::iCount here ****/
}

:

- ?

+5

++ . .

, ; , .

+3

++. cpp. / . ( main());

//main.h

class Myclass{

    static int iCount;
}; // and don't forget this ";" after a class declaration


//main.cpp

int Myclass::iCount=1;

int main(){



}
+3

++ (§8.5/10):

An initializer for a static member is in the scope of the member’s class.

class Myclass , - main.

+2

, main .

, . .

+1

All Articles