Skip to content

Instantly share code, notes, and snippets.

@drodil
Last active October 1, 2018 05:40
Show Gist options
  • Save drodil/54138be5e81ae8727b88fb22d56f2fd4 to your computer and use it in GitHub Desktop.
Save drodil/54138be5e81ae8727b88fb22d56f2fd4 to your computer and use it in GitHub Desktop.
#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