添え字演算子

#include <iostream>
#include <cstring>

using namespace std;

class Array {
private:
  int _size;
  int *elements;

public:
  Array(int size);
  ~Array();
  int size();
  int &operator [](int i);
};

Array::Array(int size) : _size(size) {
  elements = new int[size];
  memset(elements, 0, sizeof(elements));
};

Array::~Array() {
  delete [] elements;
}

int Array::size() {
  return _size;
}

int &Array::operator [](int i) {
  return elements[i];
}

int main() {
  Array ary(10);

  for (int i = 0; i < ary.size(); i++) {
    ary[i] = i * i;
  }

  for (int i = 0; i < ary.size(); i++) {
    cout << ary[i] << "\n";
  }

  return 0;
}


0
1
4
9
16
25
36
49
64
81