what is redefining static variable in c++?

68 Views Asked by At

It's said that, Once the definition for static data member is made, user cannot redefine it. what does it exactly mean? As I am able to assign anything to it

#include<iostream.h>
class X
{
    public:
       static int i;
    X()
    {
        i = 2;    //no error in initializing it in constructor
    };
};

int X::i=1;

int main()
{
   X obj;
   cout << obj.i;   // prints 2, no prob. occur
   obj.i = 3;
   cout << obj.i;   // prints 3, same runs fine 
   X::i = 10;
   cout<<obj.i;     // prints 10, still no error
   return 0;
}

if assigning value and redefining are different, then what is redefining a static or a even a normal variable?

Thanks in advance!

0

There are 0 best solutions below