design_patterns/05_singleton.cpp
Yuki f7eac92f68
feat: Singleton.
Implemented a demo for the Singleton.
2025-04-05 22:58:40 +08:00

25 lines
441 B
C++

#include <iostream>
using namespace std;
class Singleton {
Singleton() {}
public:
// Copy construction and assignment is disabled;
Singleton(Singleton &other) = delete;
void operator=(const Singleton &) = delete;
static Singleton& get_instance() {
static Singleton s;
return s;
}
void f() {
cout << "Singleton::f()" << endl;
}
};
int main() {
Singleton::get_instance().f();
}