The this Pointer

Implementing the Composite Pattern

How can I use the this pointer to implement the composite design pattern?

The Composite pattern is used to compose objects into tree structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions uniformly. Let's implement this pattern using the this pointer in C++:

#include <iostream>
#include <memory>
#include <vector>

class Component {
 public:
  virtual ~Component() = default;
  virtual void operation() = 0;
  virtual void add(
    std::shared_ptr<Component> component) {}
  virtual void remove(
    std::shared_ptr<Component> component) {}
  virtual Component* getChild(int index) {
    return nullptr;
  }
};

class Leaf : public Component {
 public:
  void operation() override {
    std::cout << "Leaf operation\n";
  }
};

class Composite : public Component {
 private:
  std::vector<std::shared_ptr<Component>> children;

 public:
  void operation() override {
    std::cout << "Composite operation\n";
    for (const auto& child : children) {
      child->operation();
    }
  }

  void add(
    std::shared_ptr<Component> component
  ) override {
    children.push_back(component);
  }

  void remove(
    std::shared_ptr<Component> component
  ) override {
    // Removal logic (omitted for brevity)
  }

  Component* getChild(int index) override {
    return children[index].get();
  }

  // Using 'this' for method chaining
  Composite* addAndReturnThis(
    std::shared_ptr<Component> component
  ) {
    add(component);
    return this;  
  }
};

int main() {
  auto root = std::make_shared<Composite>();
  auto branch1 = std::make_shared<Composite>();
  auto branch2 = std::make_shared<Composite>();
  auto leaf1 = std::make_shared<Leaf>();
  auto leaf2 = std::make_shared<Leaf>();
  auto leaf3 = std::make_shared<Leaf>();

  root->addAndReturnThis(branch1)
    ->addAndReturnThis(branch2);  

  branch1->add(leaf1);
  branch2->addAndReturnThis(leaf2)
    ->addAndReturnThis(leaf3);

  root->operation();
}
Composite operation
Composite operation
Leaf operation
Composite operation
Leaf operation
Leaf operation

In this implementation, we use the this pointer in the addAndReturnThis() method of the Composite class. This method adds a component to the composite and then returns this, allowing for method chaining.

The this pointer is crucial here because:

  1. It allows us to return a pointer to the current instance (Composite*).
  2. This enables method chaining, making our client code more readable and concise.

In the main() function, we demonstrate how this method chaining works:

root->addAndReturnThis(branch1)
    ->addAndReturnThis(branch2);

This line adds branch1 to root, then immediately adds branch2 to root as well.

Similarly, we can chain multiple addAndReturnThis() calls on branch2:

branch2->addAndReturnThis(leaf2)
       ->addAndReturnThis(leaf3);

This pattern is particularly useful when you're dealing with tree-like structures, such as GUI components, file systems, or organizational hierarchies. It allows you to treat complex and primitive objects uniformly, simplifying client code.

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