Skip to content

Instantly share code, notes, and snippets.

@j0d5
Created January 19, 2014 16:40
Show Gist options
  • Save j0d5/8507328 to your computer and use it in GitHub Desktop.
Save j0d5/8507328 to your computer and use it in GitHub Desktop.
Implementation of an singleton in c++
/*
* Filename: singleton.cpp
* Description: singleton
*
* Created: 06/12/2011 13:31:14
* Compiler: gcc
*
* Author: Johannes82
*
*/
#include <iostream>
using namespace std;
class Singleton {
private:
Singleton() {};
~Singleton() {};
static Singleton* instance;
public:
static Singleton& getInstance();
};
Singleton* Singleton::instance = 0;
Singleton& Singleton::getInstance() {
if (instance == NULL) {
cout << "New instance!" << endl;
instance = new Singleton();
} else
cout << "No new instance!" << endl;
return *instance;
}
int main (void) {
Singleton* singleton1;
singleton1->getInstance();
Singleton* singleton2;
singleton2->getInstance();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment