static inside local scope in c 2b 2b

Solutions on MaxInterview for static inside local scope in c 2b 2b by the best coders in the world

showing results for - "static inside local scope in c 2b 2b"
Eli
22 Mar 2020
1#include<iostream>
2//Singleton class is a class having only one instance
3class SingleTon {
4
5public:
6	static SingleTon& Get() {
7		static SingleTon s_Instance;
8		return s_Instance;
9	}//there is only one instance of static functions and variables across all instances of class
10	void Hellow() {}
11};
12void Increment() {
13	int i = 0;//The life time of variable is limited to the function scope
14	i++;
15	std::cout << i << std::endl;
16};//This will increment i to one and when it will reach the end bracket the lifetime of var will get  destroyed
17void IncrementStaticVar() {
18	static int i = 0;//The life time of this var is = to program
19	i++;
20	std::cout << i << std::endl;
21}//This will increment i till the program ends
22int main() {
23	
24	Increment();//output 1
25	Increment();//output 1
26	Increment();//output 1
27	IncrementStaticVar();// output 2
28	IncrementStaticVar();// output 3
29	IncrementStaticVar();// output 4
30	IncrementStaticVar();// output 5
31	SingleTon::Get();
32	std::cin.get();
33
34}