Last active
October 1, 2018 05:40
-
-
Save drodil/54138be5e81ae8727b88fb22d56f2fd4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
#include <string> | |
class Singleton { | |
public: | |
// Returns a reference to static instance of this class | |
static Singleton& instance() { | |
static Singleton instance; | |
return instance; | |
} | |
// The big five. We don't want singleton instances to be | |
// copyable nor movable. They should always be accessed with | |
// ::instance. | |
~Singleton() = default; | |
Singleton(Singleton&) = delete; | |
Singleton(Singleton&&) = delete; | |
Singleton& operator=(const Singleton&) = delete; | |
Singleton& operator=(const Singleton&&) = delete; | |
void say_hello_world() { | |
std::cout << "Hello world!" << std::endl; | |
} | |
private: | |
Singleton() = default; | |
}; | |
int main() | |
{ | |
auto& inst = Singleton::instance(); | |
inst.say_hello_world(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment