Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does subscripting std::array multiple times work?

How does subscripting multiple times work for std::array even though all operator[]returns is a reference, without using any proxy-objects (as shown here)?

Example:

#include <iostream>
#include <array>

using namespace std;

int main()
{

    array<array<int, 3>, 4> structure;
    structure[2][2] = 2;
    cout << structure[2][2] << endl;

    return 0;
}

How/why does this work?

like image 934
TeaOverflow Avatar asked Sep 14 '26 00:09

TeaOverflow


1 Answers

You simply call structure.operator[](2).operator[](2), where the first operator[] returns a reference to the third array in structure to which the second operator[] is applied.

Note that a reference to an object can be used exactly like the object itself.

like image 61
Baum mit Augen Avatar answered Sep 16 '26 01:09

Baum mit Augen