-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
37 lines (29 loc) · 849 Bytes
/
main.cpp
File metadata and controls
37 lines (29 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
class Singleton {
private:
static Singleton* instance;
// Private constructor
Singleton() {
std::cout << "Singleton instance created.\n";
}
// Delete copy constructor and assignment
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
public:
static Singleton* getInstance() {
if (instance == nullptr)
instance = new Singleton();
return instance;
}
void showMessage() {
std::cout << "Hello from Singleton!" << std::endl;
}
};
Singleton* Singleton::instance = nullptr;
int main() {
Singleton* s1 = Singleton::getInstance();
s1->showMessage();
Singleton* s2 = Singleton::getInstance();
std::cout << (s1 == s2); // Will print 1 (true)
return 0;
}