Created
January 18, 2017 20:58
-
-
Save colesnicov/f506685deedea1f1588c0304a1809826 to your computer and use it in GitHub Desktop.
Singleton in C++
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
/** | |
* @file Singleton.cpp part of @file Singleton.hpp | |
* @version 2 | |
* | |
* @author Denis Colesnicov <eugustus@gmail.com> | |
* @date 2017/01/15 | |
* | |
* @copyright WTFPL | |
*/ | |
#include "Singleton.h" | |
Singleton* Singleton::m_instance = nullptr; | |
Singleton* Singleton::getInstance() | |
{ | |
if(!m_instance){ | |
m_instance = new Singleton; | |
} | |
return m_instance; | |
} |
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
#pragma once | |
/** | |
* @file Singleton.hpp | |
* @version 2 | |
* | |
* @brief Realisation of pattern <SINGLETON> | |
* @details Usage for Loggers, Loaders and similar possible situations. | |
* | |
* @author Denis Colesnicov <eugustus@gmail.com> | |
* @date 2017/01/15 | |
* | |
* @copyright WTFPL | |
*/ | |
class Singleton{ | |
public: | |
/** | |
* @brief Create and return instance of singleton | |
* @pre The initialization occurs only once, then it will return the same instance of the class. | |
* @return Singleton* | |
*/ | |
static Singleton* getInstance(); | |
private: | |
Singleton(){}; | |
Singleton(Singleton const&){}; | |
Singleton& operator=(Singleton const&){}; | |
static Singleton* m_instance; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment