Singleton Header

namespace singleton_pattern

Namespace for the Singleton pattern.

class Singleton
#include <singleton.hpp>

A class implementing the Singleton design pattern.

The Singleton class ensures that only one instance of the class exists and provides a global point of access to that instance. This is achieved using a private constructor and a static method for instance retrieval.

Public Functions

void doSomething()

An example method to demonstrate Singleton usage.

Example method that demonstrates the Singleton’s behavior.

This method serves as an example of how the Singleton can provide functionality via its single instance.

This method serves as an example of how to interact with the Singleton instance. It performs a specific action, such as printing a message.

Public Static Functions

static Singleton &getInstance()

Retrieves the single instance of the Singleton class.

Retrieves the Singleton instance.

This method returns a reference to the single instance of the Singleton. The instance is created lazily and is guaranteed to be thread-safe in C++11 and later.

This static method ensures that only one instance of the Singleton class exists throughout the program’s lifecycle. The instance is lazily initialized on its first use, and its lifetime is automatically managed by the program.

The static local variable ensures thread safety in C++11 and later standards.

Returns:

Singleton& A reference to the Singleton instance.

Returns:

A reference to the Singleton instance.

Private Functions

Singleton()

Constructs the Singleton instance.

The constructor is private to prevent direct instantiation of the Singleton class.

This constructor is private to enforce the Singleton pattern, ensuring that no external code can directly instantiate the class. Any necessary initialization logic for the Singleton instance can be added here.

~Singleton()

Destroys the Singleton instance.

The destructor is private to prevent direct destruction of the Singleton instance.

This destructor is private to enforce the Singleton pattern, ensuring that no external code can directly destroy the instance. Any cleanup logic for the Singleton instance can be added here.

Singleton(const Singleton&) = delete

Deleted copy constructor.

Copying the Singleton instance is disabled to ensure only one instance exists.

Singleton &operator=(const Singleton&) = delete

Deleted assignment operator.

Assigning one Singleton instance to another is disabled to ensure only one instance exists.

Returns:

Singleton& Reference to the Singleton instance.

Inner Classes

Inner Namespaces