Implementing the operator [] overload for an array class is easy, if you need to simply get the array element, and how to implement operator [] overloading to implement the form:

 intArray a(5); a[0]=5; a[1]=10; // и т.д. 

I would be grateful for the answer or link to the answer.

  • The issue is resolved, thanks for the help :) - kariishka

2 answers 2

If there is a class and it is necessary to overload the оператор [] for class elements of type T , then the operator is usually overloaded as follows

 T & operator []( size_t ); 

and

 const T & operator []( size_t ) const; 

The first operator allows you to change the class element passed by reference, and the second allows you to work with constant objects.

Note that you should simultaneously define both of these operators in a class.

For scalar types, when copying objects is a simple operation, then the second operator can also be defined as

 T operator []( size_t ) const; 

That is, it will return a temporary object.

As I understand it, for your case the type T will be the type int .

You can also overload the operator when the parameter is an initialization list.

Below is a demonstration program.

 #include <iostream> #include <algorithm> #include <initializer_list> class Array { public: Array() : n(0), a(nullptr) {} explicit Array(size_t n) : n(n), a(nullptr) { if (n) a = new int[n](); } Array(size_t n, int value ) : n(n), a(nullptr) { if (n) { a = new int[n]; std::fill(a, a + n, value); } } Array(const Array &a) : n(0), a(nullptr) { if (an) { this->a = new int[an]; this->n = an; std::copy(a.begin(), a.end(), this->begin()); } } ~Array() { delete a; } size_t size() const { return n; } const int & operator [](size_t i) const { return a[i]; } int & operator [](size_t i) { return a[i]; } Array operator [](std::initializer_list<size_t> lst) const { Array a(lst.size()); size_t i = 0; for ( size_t j : lst ) { aa[i++] = this->a[j]; } return a; } int * begin() { return a; } const int * begin() const { return a; } int * end() { return a + n; } const int * end() const { return a + n; } private: size_t n; int *a; }; int main() { const int N = 10; Array a(N); int i = N; for (auto &x : a) x = --i; for (auto x : a) std::cout << x << ' '; std::cout << std::endl; Array b = a[{ 0, 2, 4, 6, 8}]; for (auto x : b) std::cout << x << ' '; std::cout << std::endl; } 

Output of the program to the console

 9 8 7 6 5 4 3 2 1 0 9 7 5 3 1 

In this example, three operator [] statements are defined operator []

  const int & operator [](size_t i) const; int & operator [](size_t i); Array operator [](std::initializer_list<size_t> lst) const; 

I determined the class at a minimum. For example, it does not have a copy assignment operator. You can write it yourself.

In order for the code to be compiled in MS Visual Studio, you need to include a macro <stdafx.h> header file as follows

 #pragma once #define _CRT_SECURE_NO_WARNINGS //... 
  • Thank you for such a detailed answer! - kariishka
  • @kariishka Not at all. Ask more. :) - Vlad from Moscow

If to obtain you define an operator as

 int operator[](size_t index); 

then in order to get the value and assign it, you need to define it as a link:

 int& operator[](size_t index);