Last active
January 18, 2019 16:51
-
-
Save shubham7298/d978f734d5fd965ea1130f09f46ef183 to your computer and use it in GitHub Desktop.
Attempt to make singleton class in C++
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 Singleton | |
{ | |
private: | |
static Singleton obj; | |
Singleton(){ | |
cout<<"Constructor does not get called"; | |
} | |
string s; | |
public: | |
int get_value() | |
{ | |
cout<<"It works"; | |
} | |
static Singleton getInstance() | |
{ | |
if(&obj == NULL) | |
Singleton* obj = new Singleton; | |
return obj; | |
} | |
}; | |
Singleton Singleton::obj; | |
int main() | |
{ | |
Singleton::getInstance().get_value(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment