Creating Custom Iterators using C++20 Concepts

Implementing Postfix Operator++ for Custom Iterators

How do I implement the postfix operator++ for a custom iterator in C++?

Abstract art representing computer programming

To implement the postfix operator++ for a custom iterator in C++, you need to define an additional version of the operator++ function.

This version is distinguished by an int parameter to differentiate it from the prefix version. The postfix increment returns the iterator's state before it was incremented.

Here's how you can implement it:

#include <iostream>
#include <string>
#include <stdexcept>

class Player {
public:
  std::string Name;
};

class Party {
public:
  Party(Player A, Player B, Player C)
    : A{A}, B{B}, C{C} {}

  Player A, B, C;

  class Iterator {
  public:
    using iterator_category = std::forward_iterator_tag;
    using value_type = Player;
    using difference_type = std::ptrdiff_t;
    using pointer = Player*;
    using reference = Player&;

    Iterator(Party* ptr = nullptr, size_t idx = 0) 
      : Party(ptr), idx(idx) {}

    Player& operator*() const {
      if (idx == 0) return Party->A;
      if (idx == 1) return Party->B;
      if (idx == 2) return Party->C;
      throw std::out_of_range("Invalid index");
    }

    Player* operator->() const {
      return &**this;
    }

    Iterator& operator++() {
      ++idx;
      return *this;
    }

    Iterator operator++(int) { 
      Iterator tmp = *this; 
      ++(*this); 
      return tmp; 
    } 

    bool operator==(const Iterator& other) const {
      return Party == other.Party && idx == other.idx;
    }

    bool operator!=(const Iterator& other) const {
      return !(*this == other);
    }

  private:
    size_t idx;
    Party* Party;
  };

  Iterator begin() { return Iterator(this, 0); }
  Iterator end() { return Iterator(this, 3); }
};

int main() {
  Party party{Player{"Anna"},
    Player{"Bob"}, Player{"Cara"}};

  auto it = party.begin();
  std::cout << (it++)->Name << "\n";  
  std::cout << (it++)->Name << "\n"; 
  std::cout << it->Name << "\n"; 
}
Anna
Bob
Cara

Explanation

  • Iterator operator++(int): This defines the postfix ++ operator. It creates a temporary copy of the current iterator, increments the iterator using the prefix ++, and returns the temporary copy.
  • Usage in main function: Demonstrates how the postfix ++ operator works by printing the names of the Player objects as the iterator advances.

This implementation ensures that your custom iterator supports both prefix and postfix increment operations, making it more versatile and compatible with standard C++ idioms.

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

A computer programmer
Part of the course:

Professional C++

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

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% 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.

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