Created
January 19, 2014 16:40
-
-
Save j0d5/8507328 to your computer and use it in GitHub Desktop.
Implementation of an singleton 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
/* | |
* 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