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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With