+ 1

Static in class vs. Static in function

What is the difference between a static class member and a static function variable?

21st Apr 2018, 7:23 PM
Jack
Jack - avatar
2 Respostas
+ 4
A static function variable remains even after the function is finished: Function: void print(){ static int x = 0; cout << x++; } Calls: for (int i=0; i<5; i++){ print(); } This prints 01234. With a non-static it would print 00000. And for members: They aren't bound to objects, but the class itself. class A{ public: void print(){ cout << "hi"; }; print can be called with A::print(); Static member variables are also bound to the class itself. If you set an integer to let's say 5, it is 5 for all created objects of the class.
21st Apr 2018, 8:01 PM
Alex
Alex - avatar
+ 2
Either have scope in they ambits (class and function) but you can use access modifier on class-member (function static var are visible only in that function) and they will be inited at start time while function static var will be inited the first time that it will be called.... More,
21st Apr 2018, 7:59 PM
KrOW
KrOW - avatar