0

what is static data member ?

5th Nov 2016, 12:06 PM
Damodar Joshi
Damodar Joshi - avatar
2 Réponses
+ 1
It's actually quite simple. If you declare a variable as static in the scope of a function, its value is preserved between successive calls to that function. So: int myFun() { static int i=5; i++; return i; } int main() { cout << myFun(); cout << myFun(); cout << myFun(); } will show 678 instead of 666, because it remembers the incremented value.
5th Nov 2016, 1:34 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 1
a static data member acts like a global variable for its class. i.e. it is globally available for all the objects of its class and can be modified by all the objects of the class. class test{ public: static int a; }; int test::a=0; int main() { test t; cout<<t.a<<endl; t.a++; cout<<t.a<<endl; test p; p.a++; cout<<p.a; } //you see variable has been modified by both objects edit: this is different explanation from what aditya has given. It is for static data member of a class. So, don't get confused.
5th Nov 2016, 1:37 PM
kamal joshi
kamal joshi - avatar