0

Why is needed static in c++? How it works? Please, give example.

14th Aug 2017, 7:51 PM
Alexik
Alexik - avatar
4 Answers
+ 8
One of the uses of static variables is recursion base case evaluation. void recur() { static int x = 0; if (x < 10) { std::cout << "Recursing.\n"; ++x; recur(); } }
15th Aug 2017, 1:42 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
static data members are just like global variable maintaining only one copy of the variable that are common to all the objects of the class. for eg,static data members can be used to keep track of existing objects.
15th Aug 2017, 4:51 AM
Swati Tiwari
0
en.cppreference.com/w/cpp/language/static I guess they cover whole topic of static keyword ^^
14th Aug 2017, 8:57 PM
Jakub Stasiak
Jakub Stasiak - avatar
0
@Await Tiwari, that is, if I declared static void f(), now I can't overload this function/method?
15th Aug 2017, 5:23 AM
Alexik
Alexik - avatar