오직 한 개의 클래스 인스턴스만을 갖도록 보장하고, 이에 대한 전역적인 접근점을 제공합니다.(GoF의 디자인 패턴 181p)
#include <iostream>
using namespace std;
class C_File_System {
public:
static C_File_System& Instance() {
static auto Instance = make_shared<C_File_System>();
return *Instance;
}
private:
C_File_System() {}
};
int main()
{
C_File_System::Instance();
}
#include <iostream>
#include <cassert>
using namespace std;
class C_File_System {
public:
C_File_System() {
assert(!instantiated_);
instantiated_ = true;
}
~C_File_System() {
instantiated_ = false;
}
private:
static bool instantiated_;
};
bool C_File_System::instantiated_ = false;