Skip to content

Instantly share code, notes, and snippets.

@shubham7298
Last active January 18, 2019 16:51
Show Gist options
  • Save shubham7298/d978f734d5fd965ea1130f09f46ef183 to your computer and use it in GitHub Desktop.
Save shubham7298/d978f734d5fd965ea1130f09f46ef183 to your computer and use it in GitHub Desktop.
Attempt to make singleton class in C++
#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