MJ wrote:
Hi
I tried the following code
--------------------------------------------------------------
class A
{
public:
static int a;
static float f() { return 3.14;}
};
int main () {
//int A::a = -1; // line 1
float g = A::f(); // line 2
cout << g << endl;
cin >> g;
return 0 ;
}
--------------------------------------------------------------
if i uncommet the line 1 its giving me an error " definition or
redeclaration illegal in current scope"
but the static function I am able to access properly and there is no
problem
if I put both the lines out side the main loop it does not give me any
error
So does it mean that the static function are accessible but static
variable are specific to scope ??
MJ
Just remove the *int* from the line 1. In main() all you want to do is alter
the static variable not re-declare it. So it would be:
int main()
{
A::a = -1;
...
}
Also, there is another bug. A::a is undefined. You say what the *initial
value* of A::a because check it as in:
int main()
{
if(A::a > 10)
cout << "A::a is greater than 10" << endl;
...
}
With your current implementation, the above code will have undefined
behaviour (that just means it will act weird).
--
Alvin