Design patterns Singleton Singleton (C++)


As per Wiki : In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object.

This is required to create exactly one object to coordinate actions across the system.

class Singleton
    // Private constructor so it can not be arbitrarily created.
    // Disable the copy and move
    Singleton(Singleton const&)            = delete;
    Singleton& operator=(Singleton const&) = delete;

    // Get the only instance
    static Singleton& instance()
        // Use static member.
        // Lazily created on first call to instance in thread safe way (after C++ 11)
        // Guaranteed to be correctly destroyed on normal application exit.
        static Singleton _instance;

        // Return a reference to the static member.
        return _instance;