As SA said the definition of static members is allowed. I was able to compile your code and it runs without any errors from the compiler. You should have described to us exactly what error your are seeing, that greatly helps us to provide the answer that you need. BTW, I am using Visual Studio 2010, but is should work the same way in other compilers.
This is what your code is doing. The static variable foo is initialized to 0. After you declare f1, f2 & f3 their constructors are called, in the constructors count is incremented. Since all three (f1, f2 & f3) are all incrementing the same variable, count winds up being equal to 3. Then when you call getcount for each one and send the result to the console you will see 3 for each one.
If you want to have the output be different for each call to getcount you need to do the increment of count somewhere other than the constructor. Or don't construct f1, f2 and f3 before doing the output. For example if you do the following you will get 1, 2, 3 for the output.
foo f1;
cout << "count is " << f1.getcount() << endl;
foo f2;
cout << "count is " << f2.getcount() << endl;
foo f3;
cout << "count is " << f3.getcount() << endl;
So that the console doesn't disappear after it runs you should add the following lines in your main method, just before the return statement:
std::cout << "Press ENTER to continue... " << flush;
std::cin.ignore( std::numeric_limits <std::streamsize xmlns:std="#unknown"> ::max(), '\n' );
</std::streamsize>
The console closing may not be an issue with all IDEs, but it does happen with Visual Studio.
[Edit- add more explanation of initialization]
From your comment you seem to be bothered by the initialization of the static class member, count. It is important to understand that a static class member cannot be initialized inside of the class. While this may seem odd, it is because a static class member really exists outside of any instance of the class. You are required to initialize a static class member or it will not be conspired to be in scope inside the class. The syntax is odd, but it does work as designed. Read
this article[
^] for some additional explanation.
[Edit- answer why static const data definition is allowed inside the class]
The variable, count, in your code is a member of the class and is therefore declared inside the class. When you make it a static class variable you are saying that every instance of the class will share that variable. So if f1 increments count the value will be one, then if f2 increments it the value will be two, if f3 looks at count next it will see a value of two.
Because count is a static class variable is exists outside of any instance of the class, in fact count exists before any instance of the class has been created. Because of this separation count must be initialized outside of the class so that count will have its initial value when the first instance of the foo class is created.