Overload [] operator for class : overload square bracket « Operator Overloading « C++ Tutorial






#include <iostream>
using namespace std;

class Point {
  int a[3];
public:
  Point(int i, int j, int k) {
    a[0] = i;
    a[1] = j;
    a[2] = k;
  }
  int operator[](int i) { return a[i]; }
};

int main()
{
  Point ob(1, 2, 3);

  cout << ob[1]; // displays 2

  return 0;
}
2"








10.10.overload square bracket
10.10.1.Overload [] operator for class
10.10.2.Provide range checking for operator []