7

I have a static variable in the class. I am Initializing that in the global scope, its works fine.

But When I try to Initialize in the main linker throws an error. Why it so.

class Myclass{

    static int iCount;
} ;

int main(){

  int Myclass::iCount=1;

}

And In global scope why I have to specify the variable type like

int Myclass::iCount=1;

As In my class I am definig iCount as integer type why not.

   Myclass::iCount =1 ; in //Global scope
Clifford
  • 88,407
  • 13
  • 85
  • 165
vrbilgi
  • 5,703
  • 12
  • 44
  • 60
  • @goreSplatter: `Myclass::iCount` is not public, it would fail. – peoro Jan 15 '11 at 10:32
  • @goreSplatter: What @peoro said, but also even if it were public, that would be an *assignment* not and *initialisation* – Clifford Jan 15 '11 at 10:37
  • @peoro : *initialization* of private static data members don't fail!! other accesses, like *assignment*, will fail. – Nawaz Jan 15 '11 at 11:03
  • @Nawaz: you didn't read the comment I was answering to. It was suggested to put a `Myclass::iCount=1;` in `main`. – peoro Jan 15 '11 at 11:05
  • @peoro: you were talking about assignment or initialization? – Nawaz Jan 15 '11 at 11:16
  • @Nawaz: assignment, since initilaziation can't be done in a function. – peoro Jan 15 '11 at 12:01

5 Answers5

5

The section $9.4.2/7 from the C++ Standard says,

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

Note the phrases "initialized" and "exactly like non-local objects". Hope that explains why you cannot do that.

In fact, static members are more like global objects accessed through Myclass::iCount. So, you've to initialize them at global scope (the same scope at which class is defined), like this:

class Myclass{

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

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

Similar topic:

How do static member variables affect object size?

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
3

Because C++ syntax doesn't allow this. You need to instantiate your static variable outside of the scope of some function.

Besides you forget a semicolon ; after your class ending bracket.

peoro
  • 25,562
  • 20
  • 98
  • 150
3

this is the correct C++. Outside of a function, in a cpp file. the initialisation is done at the beginning/launching of the executable. ( even before calling 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(){



}
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
2

From C++ standard (§8.5/10):

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

class Myclass has global scope and you tried to initialize its static member in the narrower scope - of the function main.

Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51
1

The static initialisation occurs before main is called by the run-time initialisation.

Placing it within a function is not allowed because that is where locally scoped objects are declared. It would be confusing and ambiguous to allow that.

Clifford
  • 88,407
  • 13
  • 85
  • 165