The this Pointer

Implementing the Singleton Pattern

How can I use the this pointer to implement the Singleton design pattern in C++?

The Singleton pattern ensures a class has only one instance and provides a global point of access to it. We can use the this pointer to implement this pattern in C++. Here's how:

#include <iostream>

class Singleton {
 private:
  static Singleton* instance;

  // Private constructor to prevent instantiation
  Singleton() {}

 public:
  // Delete copy constructor and assignment operator
  Singleton(const Singleton&) = delete;
  Singleton& operator=(const Singleton&) = delete;

  static Singleton* getInstance() {
    if (instance == nullptr) {
      instance = new Singleton();
    }
    return instance;
  }

  void someBusinessLogic() {
    std::cout << "Singleton is doing something.\n";
  }

  // Use this pointer to provide instance methods
  Singleton* doSomething() {
    std::cout << "Did something and "
      "returning this.\n";
    return this;  
  }

  Singleton* doSomethingElse() {
    std::cout << "Did something else and "
      "returning this.\n";
    return this;  
  }
};

// Initialize the static member
Singleton* Singleton::instance = nullptr;

int main() {
  Singleton* s = Singleton::getInstance();
  s->someBusinessLogic();

  // Chain method calls using 'this'
  s->doSomething()->doSomethingElse();

  return 0;
}
Singleton is doing something.
Did something and returning this.
Did something else and returning this.

In this implementation, we use the this pointer in two ways:

  1. To implement the Singleton pattern itself: The getInstance() method creates the single instance if it doesn't exist, and returns a pointer to it.
  2. To enable method chaining: The doSomething() and doSomethingElse() methods return this, allowing us to chain these method calls.

By returning this, we're returning a pointer to the current instance, which allows us to call another method on the same instance immediately. This creates a fluent interface, making the code more readable and expressive.

Remember, the Singleton pattern should be used sparingly, as it can make your code harder to test and maintain. However, it can be useful in scenarios where you need to ensure that a class has only one instance throughout the application's lifecycle, such as for managing a shared resource or coordinating actions across the system.

This Question is from the Lesson:

The this Pointer

Learn about the this pointer in C++ programming, focusing on its application in identifying callers, chaining functions, and overloading operators.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

The this Pointer

Learn about the this pointer in C++ programming, focusing on its application in identifying callers, chaining functions, and overloading operators.

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

This course includes:

  • 60 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

View Course
Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2025 - All Rights Reserved