Skip to content

Instantly share code, notes, and snippets.

@colesnicov
Created January 18, 2017 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colesnicov/f506685deedea1f1588c0304a1809826 to your computer and use it in GitHub Desktop.
Save colesnicov/f506685deedea1f1588c0304a1809826 to your computer and use it in GitHub Desktop.
Singleton in C++
/**
* @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;
}
#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