D
D
Dima Sokolov2018-04-25 22:32:39
C++ / C#
Dima Sokolov, 2018-04-25 22:32:39

Overloading [] on Write In C++?

Is it possible to make it so that with the help of an indexer it was possible to assign a value, and not just read it?
MyClass a[5] = {1,2,2}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
devalone, 2018-04-25
@dimka11

#include <iostream>

template <typename T, size_t N>
class TestArray {
public:
  T& operator[](size_t i) {
    return data[i];
  }
private:
  T data[N];
};

int main() {
  TestArray<int, 10> arr;
  arr[0] = 99;
  arr[1] = -1;
  std::cout << arr[0] << std::endl;
  std::cout << arr[1] << std::endl;
  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question