Add example for classic singleton
This commit is contained in:
25
cpp/patterns/singleton/singleton.hpp
Normal file
25
cpp/patterns/singleton/singleton.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
#ifndef SINGLETON_H
|
||||
#define SINGLETON_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class Singleton {
|
||||
public:
|
||||
static Singleton &getInstance();
|
||||
~Singleton();
|
||||
|
||||
inline void showMessage() const { std::cout << message;}
|
||||
inline void updateMessage(const std::string &m) { message = m;}
|
||||
|
||||
private:
|
||||
std::string message;
|
||||
|
||||
// Don't allow copying of this class
|
||||
Singleton() { message = "New singleton\n";}
|
||||
Singleton(const Singleton &) {}
|
||||
Singleton &operator=(const Singleton &) {}
|
||||
};
|
||||
|
||||
#endif // SINGLETON_H
|
||||
Reference in New Issue
Block a user