Created
January 13, 2018 06:42
-
-
Save slwu89/6aaff97e19d02067913dafd64be00020 to your computer and use it in GitHub Desktop.
thread safe c++11 singleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class CSingleton final | |
{ | |
public: | |
static CSingleton* GetInstance(); | |
void set_x(const int& x_){x = x_;}; | |
int get_x(){return x;}; | |
private: | |
int x; | |
CSingleton() = default; | |
~CSingleton() = default; | |
CSingleton(const CSingleton&) = delete; | |
CSingleton& operator=(const CSingleton&) = delete; | |
CSingleton(CSingleton&&) = delete; | |
CSingleton& operator=(CSingleton&&) = delete; | |
}; | |
CSingleton* CSingleton::GetInstance() | |
{ | |
static CSingleton instance; | |
return &instance; | |
} | |
int main() | |
{ | |
cout<<"Hello World\n"; | |
CSingleton::GetInstance()->set_x(5); | |
std::cout << CSingleton::GetInstance()->get_x() << std::endl; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment