Search code examples
c++arraysc++11templatescopy-constructor

What does array<int,2> dim mean in this piece of code?


I came across this piece of code while reading The c++ Programming Language 4th edition

template<class T>
class Matrix {
    array<int,2> dim; // two dimensions

    T∗ elem; // pointer to dim[0]*dim[1] elements of type T

public:
    Matrix(int d1, int d2) :dim{d1,d2}, elem{new T[d1∗d2]} {} // error handling omitted

    int size() const { return dim[0]∗dim[1]; }

    Matrix(const Matrix&); // copy constructor

    Matrix& operator=(const Matrix&); // copy assignment

    Matrix(Matrix&&); // move constructor

    Matrix& operator=(Matrix&&); // move assignment

    ˜Matrix() { delete[] elem; }
    // ...
};

There are two data members in the class of which one is a pointer of type T. I am not able to understand what array< int, 2 > dim means.


Solution

  • This is making use of the std::array from the standard library. You can find a detailed reference here: https://en.cppreference.com/w/cpp/container/array

    array<int,N> x;

    declares an array of integers of length N; N is 2 in your case.

    This is later used to store the shape of your matrix.