Skip to content

Commit

Permalink
commit 1
Browse files Browse the repository at this point in the history
  • Loading branch information
HendEmad committed May 4, 2024
1 parent f622471 commit 9b34c74
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\CodeBlocks\\MinGW\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
57 changes: 57 additions & 0 deletions Singleton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>
#include <vector>
#include <string>

class Logger{
public:
//Retrive the single instance of the object created, and we'll enforce
// only one object to be created
static Logger& GetInstance(){
static Logger* s_instance = new Logger;
// static Logger* s_instance;
// if (s_instance == nullptr)
// s_instance = new Logger;
return *s_instance;
}

void printMessages()
{
std::cout << "Acessing the logger\n";
for(auto& e : m_messages){
std::cout << e << " ";
}
}

void add_message(std::string s){
m_messages.push_back(s);
}

private:
Logger(){
std::cout << "Logger is created.\n";
}

~Logger(){
std::cout << "Logger is destructed.\n";
}

std::vector<std::string> m_messages;

};


int main(){
// Logger logger1;
// Logger logger2;
// Logger logger3;
// Logger logger4;
// Logger logger5;
// Logger logger6;
Logger::GetInstance().printMessages();
Logger::GetInstance().add_message("Hello, message 1\n");
Logger::GetInstance().add_message("Hello, message 2\n");
Logger::GetInstance().add_message("Hello, message 3\n");
Logger::GetInstance().add_message("Hello, message 4\n");
Logger::GetInstance().printMessages();
return 0;
}

0 comments on commit 9b34c74

Please sign in to comment.