-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Log error / warn / info level with class 2. Static variable in class 2.1. Local 2.2. Class
- Loading branch information
Showing
6 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <iostream> | ||
|
||
class Log | ||
{ | ||
public: | ||
const int LogLevelError = 0; | ||
const int LogLevelWarning = 1; | ||
const int LogLevelInfo = 2; | ||
|
||
private: | ||
int m_LogLevel = LogLevelInfo; | ||
|
||
public: | ||
void setLevel(int level) | ||
{ | ||
m_LogLevel = level; | ||
return; | ||
} | ||
void Error(const char *message) | ||
{ | ||
if (m_LogLevel >= LogLevelError) | ||
std::cout << "[ERROR] : " << message << std::endl; | ||
return; | ||
} | ||
void Warn(const char *message) | ||
{ | ||
if (m_LogLevel >= LogLevelWarning) | ||
std::cout << "[WARN] : " << message << std::endl; | ||
return; | ||
} | ||
void Info(const char *message) | ||
{ | ||
if (m_LogLevel >= LogLevelInfo) | ||
std::cout << "[INFO] : " << message << std::endl; | ||
return; | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
Log log; | ||
log.setLevel(log.LogLevelWarning); | ||
log.Error("Hello"); | ||
log.Warn("Hello"); | ||
log.Info("Hello"); | ||
// std::cin.get(); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <iostream> | ||
|
||
struct Entity | ||
{ | ||
// int x, y; // Normal way | ||
// static int x, y; // Type 1, 2 | ||
int x, y; // Type 3, 4 | ||
|
||
// void Print() | ||
// { | ||
// std::cout << x << ", " << y << std::endl; | ||
// } // Normal, Type 1 | ||
|
||
// Static function can't call non-static variables! | ||
// static void Print() | ||
// { | ||
// std::cout << x << ", " << y << std::endl; | ||
// } // Type 2, 3 | ||
}; | ||
|
||
static void Print(Entity e) | ||
{ | ||
std::cout << e.x << ", " << e.y << std::endl; | ||
|
||
return; | ||
} // Type 4 | ||
|
||
// Static variables share their value | ||
// int Entity::x; // Type 1, 2 | ||
// int Entity::y; // Type 1, 2 | ||
|
||
int main() | ||
{ | ||
Entity e1; | ||
e1.x = 2; | ||
e1.y = 3; | ||
// Entity::x = 2; // Type 1 | ||
// Entity::y = 3; // Type 1 | ||
|
||
Entity e2; | ||
e2.x = 5; | ||
e2.y = 8; | ||
// Entity::x = 5; // Type 1 | ||
// Entity::y = 8; // Type 1 | ||
|
||
// e1.Print(); // Normal, Type 1 | ||
// Normal way : 2, 3 | ||
// Type 1 : 5, 8 | ||
// e2.Print(); // Normal, Type 1 | ||
// Normal way : 5, 8 | ||
// Type 1 : 5, 8 | ||
|
||
// Entity::Print(); // Type 2 | ||
// Entity::Print(); // Type 2 | ||
|
||
Print(e1); // Type 4 | ||
Print(e2); // Type 4 | ||
|
||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// // Function Version | ||
// #include <iostream> | ||
|
||
// void plus1() | ||
// { | ||
// static int i = 0; | ||
// i++; | ||
// std::cout << i << std::endl; | ||
// } | ||
|
||
// int main() | ||
// { | ||
// plus1(); | ||
// plus1(); | ||
// plus1(); | ||
// plus1(); | ||
// plus1(); | ||
// } | ||
|
||
// // Class Version 1 | ||
// #include <iostream> | ||
// class Singleton | ||
// { | ||
// private: | ||
// static Singleton *s_Instance; | ||
|
||
// public: | ||
// static Singleton &Get() | ||
// { | ||
// return *s_Instance; | ||
// } | ||
// void Hello() | ||
// { | ||
// } | ||
// }; | ||
|
||
// Singleton *Singleton::s_Instance = nullptr; | ||
|
||
// int main() | ||
// { | ||
// Singleton s; | ||
// s.Get().Hello(); | ||
// return 0; | ||
// } | ||
|
||
// Class Version 2 | ||
#include <iostream> | ||
#include <typeinfo> | ||
|
||
class Singleton | ||
{ | ||
private: | ||
int s_Num = 0; | ||
|
||
public: | ||
static Singleton &Get() | ||
{ | ||
static Singleton *instance; | ||
return *instance; | ||
} | ||
void Set(int num) | ||
{ | ||
s_Num = num; | ||
} | ||
void Show() | ||
{ | ||
std::cout << s_Num << std::endl; | ||
} | ||
void Hello() | ||
{ | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
Singleton s; | ||
s.Show(); | ||
s.Set(3); | ||
s.Show(); | ||
s.Get().Set(2); | ||
s.Get().Show(); | ||
|
||
return 0; | ||
} |