-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleton.cpp
55 lines (46 loc) · 1.24 KB
/
Singleton.cpp
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
/**
* \file Singleton.cpp
* \brief Singleton - only one instance of a class
*
* Singleton ensures that there is one, and only one instance of a class.
* Like global variables, everybody has access to that instance.
* Singleton should be used sparingly since the assumption that
* there should be just one of something usually ends up being wrong.
* Singleton is hard to write unit tests with.
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//-------------------------------------------------------------------------------------------------
class HelloWorld
{
public:
void test()
{
std::cout << "Hello world!" << std::endl;
}
static HelloWorld & get()
{
static HelloWorld singleton;
return singleton;
}
private:
HelloWorld() = default;
HelloWorld(const HelloWorld &);
};
//-------------------------------------------------------------------------------------------------
void
helloWorld()
{
HelloWorld::get().test();
}
//-------------------------------------------------------------------------------------------------
int main()
{
::helloWorld();
return EXIT_SUCCESS;
}
//-------------------------------------------------------------------------------------------------
#if OUTPUT
Hello world!
#endif