You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+25-8Lines changed: 25 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,8 @@ A great issue with singletons normally is that they make unit testing very hard.
10
10
To start using this library, include the `singleton.hpp` file into your code, and inherit from the `Singleton` class in your code and get its instance as follows:
11
11
12
12
```cpp
13
+
#include<singleton.hpp>
14
+
13
15
classMySingleton : publicSingleton<MySingleton>
14
16
{
15
17
private:
@@ -87,40 +89,55 @@ protected:
87
89
// A protected allows a mock to subclass this.
88
90
MySingleton()
89
91
{
92
+
++m_allocCounter;
90
93
}
91
94
friend BaseType;
92
-
93
-
virtual int MyFunction();
95
+
static int m_allocCounter;
96
+
public:
97
+
virtual int GetAllocCount()
98
+
{
99
+
return m_allocCounter;
100
+
}
94
101
};
102
+
int MySingleton::m_allocCounter = 0;
95
103
96
104
int main()
97
105
{
98
106
// This returns the constructed singleton.
99
107
auto& instance = MySingleton::Get();
100
108
101
109
// This returns the real implementation's result.
102
-
auto realResult1 = instance.MyFunction();
110
+
if (instance.GetAllocCount() != 1)
111
+
return 1;
103
112
104
113
// This reconstructs the singleton instance and returns the same instance as earlier.
0 commit comments