-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathsingleton_class.cpp
More file actions
56 lines (44 loc) · 1.67 KB
/
singleton_class.cpp
File metadata and controls
56 lines (44 loc) · 1.67 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
using namespace std;
/*
singleton class in C++
-It's a kind of design pattern which says that a class is called single if it can have only one instance at a time.
-Many classes are singleton:
--President of India
--PM of India
--Director of IIEST, Shibpur
....
--How to implement a singleton class ? How to restrict that user can only create one instance ?
Ans : It's easy to design a class which doesn't have an instance. You just have to make the constructor private.
But, only one object ? How to do this ?
It can be done easily using static data members and static member functions.
See example below
*/
class Printer {
private:
Printer() {
cout << "Printer constructed" << endl;//Private Printer cannot be constructed!!
}
static Printer* myPrinter_;//Instance of the singleton Printer (Pointer to object)
//static means this belongs to class but not to any object
public:
~Printer() {
cout << "Destructor called" << endl;
}
static const Printer& printer() { //Why reference ? We don't want a copy
if(!myPrinter_) {
myPrinter_ = new Printer(); //Since printer() is a member function, hence it can access private constructor
}
return *myPrinter_;
}
void print(int np) const { //Why const ? Because my object(myPrinter_) is returned as const Printer& (const reference)
cout << "printing " << np << " pages" << endl;
}
};
Printer* Printer::myPrinter_ = NULL;
int main() {
Printer::printer().print(10);
Printer::printer().print(20);
Printer::printer().~Printer();
return 0;
}