1#include <iostream>
2
3using namespace std;
4
5class Singleton {
6 static Singleton *instance;
7 int data;
8
9 // Private constructor so that no objects can be created.
10 Singleton() {
11 data = 0;
12 }
13
14 public:
15 static Singleton *getInstance() {
16 if (!instance)
17 instance = new Singleton;
18 return instance;
19 }
20
21 int getData() {
22 return this -> data;
23 }
24
25 void setData(int data) {
26 this -> data = data;
27 }
28};
29
30//Initialize pointer to zero so that it can be initialized in first call to getInstance
31Singleton *Singleton::instance = 0;
32
33int main(){
34 Singleton *s = s->getInstance();
35 cout << s->getData() << endl;
36 s->setData(100);
37 cout << s->getData() << endl;
38 return 0;
39}