diff --git a/Local static/Singleton.cpp b/Local static/Singleton.cpp new file mode 100644 index 0000000..bd006b2 --- /dev/null +++ b/Local static/Singleton.cpp @@ -0,0 +1,29 @@ +#include + +class Singleton +{ //WITHOUT STATIC LOCAL + private : + static Singleton* s_Instance; + public : + static Singleton& Get() { return *s_Instance; } + + void Hello() {} + +}; + +Singleton* Singleton::s_Instance = nullptr; //DECLARED + + + +int main() +{ + Singleton::Get().Hello(); + + std::cin.get(); + +} + + + + +//a class should have only one instance in existence \ No newline at end of file diff --git a/Local static/main.cpp b/Local static/main.cpp new file mode 100644 index 0000000..0a46c7b --- /dev/null +++ b/Local static/main.cpp @@ -0,0 +1,33 @@ +#include + +//static int i = 0; //static doesnt matter here by keeping ;problem is this i can be accesable anywhere + +void Funtion() +{ + //static int i = 0; same when we taken int i = 0 outside + + static int i = 0; + i++; + std::cout << i << std::endl; +} + +int main() +{ + Funtion(); + //i = 10; //funtion calls when we keep static var outside + Funtion(); + Funtion(); + Funtion(); + Funtion(); + + std::cin.get(); +} + + +//we can use static in local scope which has 2 different considerations lifetime and scope +//scope means where we can access variable +//in funtion variable is local to the declared funtion +//static local variable gives lifetime until our program ends and only can be accessed inside funtion only +//static in a funtion scope and funtion has same lifetime but different scope in class the var can acccessable in whole class by anything where as in funtion,only accesable in it!! +// without static keyword in funtion we create variable everysingle time we set to 0 and increment by 1 +//we can give static in local scope for not getting accesable by everyone diff --git a/Local static/singleton2.cpp b/Local static/singleton2.cpp new file mode 100644 index 0000000..f1d7563 --- /dev/null +++ b/Local static/singleton2.cpp @@ -0,0 +1,32 @@ +#include + +class Singleton +{ //WITHOUT STATIC LOCAL + + public : + static Singleton& Get() //BY EVERY TIME YOU CALL GET first it construct singleton ionstance then returns existing instance + + { + static Singleton Instance; + return Instance; + + } //WITHOUT STATIC IN FUNTION AS SOON AS SCOPE ENDS SINGLETON DESTROYS[STACK] + + + void Hello() {} + +}; + +int main() +{ + Singleton::Get().Hello(); + + std::cin.get(); + +} + + + + +//a class should have only one instance in existence +//it helps by replacing initialisation funtions where you might call static initialisation funtion at some pt of all ur objects so if use static get methods we can simplify \ No newline at end of file diff --git a/Local static/static.cpp b/Local static/static.cpp new file mode 100644 index 0000000..fa0cd64 --- /dev/null +++ b/Local static/static.cpp @@ -0,0 +1,22 @@ +#include + +void Funtion() +{ + static int i = 0; + i++; + std::cout << i << std::endl; +} + +int main() +{ + Funtion(); + Funtion(); + Funtion(); + Funtion(); + Funtion(); + + std::cin.get(); +} + + +//in this i variable only local to funtion cant be accesable outside