Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Local static/Singleton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>

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
33 changes: 33 additions & 0 deletions Local static/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>

//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
32 changes: 32 additions & 0 deletions Local static/singleton2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>

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
22 changes: 22 additions & 0 deletions Local static/static.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>

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