Add example for classic singleton

This commit is contained in:
2021-05-11 12:21:03 -04:00
parent 248e48d5c9
commit 9bb2f9867d
6 changed files with 65 additions and 3 deletions

View File

@@ -0,0 +1,29 @@
#ifndef SINGLETON_POINTER_HPP
#define SINGLETON_POINTER_HPP
#include <iostream>
#include <string>
class ClassicSingleton{
public:
// returns pointer to single getInstance
static ClassicSingleton* getInstance();
// example functions manipulating singleton object
inline void showMessage() const { std::cout << message;}
void updateMessage(const std::string &m) { message=m;}
private:
// Do not allow construction of this object; Use getInstance() instead
ClassicSingleton(){ message = "New ClassicSingleton\n";}
// Do not allow copying of this object
ClassicSingleton(const ClassicSingleton&){}
ClassicSingleton& operator=(const ClassicSingleton&){}
// Static pointer to instance of this singleton
static ClassicSingleton* instance;
std::string message;
};
#endif // SINGLETON_POINTER_HPP