-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsingleton.cpp
40 lines (31 loc) · 1.13 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
#include <iostream>
class Singleton
{
// Public methods
public :
static Singleton& getInstance(void) {
// This is thread-safe since C++11
static Singleton instance;
return instance;
}
void setID(int p_id) { m_id = p_id; }
int getID(void) const { return m_id; }
// Private methods
private :
Singleton() : m_id(0) {} /*!< Cstr */
~Singleton() {} /*!< Dstr */
Singleton(const Singleton&) = delete; /*!< Cpy cstrc */
Singleton(Singleton&&) = delete; /*!< Mve cstrc */
Singleton& operator=(const Singleton&) = delete; /*!< Cpy assign */
Singleton& operator=(Singleton&&) = delete; /*!< Mve assign */
// Private attributes (used as example only)
private :
int m_id;
};
int main() {
Singleton::getInstance().setID(5);
std::cout << "Singleton ID is now " << Singleton::getInstance().getID() << std::endl;
Singleton::getInstance().setID(0);
std::cout << "Singleton ID is now " << Singleton::getInstance().getID() << std::endl;
return 0;
}