-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton.cpp
More file actions
40 lines (33 loc) · 812 Bytes
/
singleton.cpp
File metadata and controls
40 lines (33 loc) · 812 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
38
39
40
#include <cstdio>
// A macro to disallow the copy constructor and operator= functions
// This should be used in the private: declarations for a class
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
void operator=(const TypeName&)
class Foo {
public:
Foo(int f);
~Foo();
private:
DISALLOW_COPY_AND_ASSIGN(Foo);
};
class Singleton {
public:
static Singleton& getInst() {
static Singleton inst;
return inst;
}
private:
explicit Singleton() { // private ctor
printf("s:ctor\n");
};
~Singleton() {
printf("s:dtor\n");
};
Singleton(Singleton&); // disable copy ctor
Singleton& operator=(Singleton&); // disable operator
};
int main(int, char**) {
Singleton& s = Singleton::getInst(); // can only be reference
return 0;
}