N-dimension array initialization
In the previous article Container assignment list I show how to assign items to STL containers using a list of items separated by a comma.
This example will show how to do the same with a N-dimensional array, with debug build bounds checking. First an example which shows how to declare and initialize a N-dimensional array of type int or type double with a comma separated list of items.
- // define a 3 dimensional array of int's
- // and initialize.
- MyArray<int, 3> intArray3D;
- intArray3D = 1, 2, 3;
- // define a 2 diemsional array of double's
- // and initialize.
- MyArray<double, 2> doubleArray2D;
- doubleArray2D = 10.0, 20.0;
- // define a 3 x 3 matrix of double and initialize
- MyArray<double, 9> doubleMatrix3x3;
- doubleMatrix3x3 = 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0;
The N-Dimensional array class implementation
The array class code below only shows enough code to get the assignment and comma operator overloaded. Other useful code and overloads that this class would have is not shown, in order to keep the discussion simple and on topic.
- template<class T, int ArraySize>
- class MyArray
- {
- public:
- MyArray()
- {
- assert(ArraySize != 0);
- // Set items in array to 0
- for(size_t i = 0; i < ArraySize; ++i)
- m_array[i] = 0;
- }
- Initializer<T, ArraySize> operator=(T val)
- {
- // store RHS of = to first array item location
- m_array[0] = val;
- // Call Initializer construct with address of m_array[1],
- // return Initializer object.
- // Allows the overloaded comma operator for Initializer
- // to be called repeatedly.
- return Initializer<T, ArraySize>(&m_array[1]);
- }
- private:
- T m_array[ArraySize];
- };
MyArray is a template class that needs 2 template parameters, the first is the type of array to create, and the second is the number of elements the array can store.
The default constructor assigns 0 to each element in the array, if this does not make sense for the type object being stored in the array then modify as needed.
The overloaded assignment operator takes the first item in the list, right of the = sign (RHS), and assigns it to first position in the array. It then calls Initializer’s constructor with the address of the second element in the array, and returns the Initializer object.
This sets up Initializer, which overloads the comma operator, to handle the rest of the assignment list. Overloading the assignment and comma operators is discussed in the article Container assignment list.
The Initializer class is simple and documented below.
- // Initializer class. Helper class for MyArray,
- // keeping track of the current element being
- // assigned, with the overloaded comma operator,
- template<class T, int ArraySize>
- class Initializer
- {
- public:
- Initializer() : m_ptr(0)
- {
- #ifndef NDEBUG
- m_nCount = 0;
- #endif
- }
- // Constructor called by MyArray operator=
- // ptr will equal MyArray::m_array[1]. Since
- // For debug builds set m_nCount to 1 since
- // MyArray already assigned element [0].
- Initializer(T * ptr) : m_ptr(ptr)
- {
- #ifndef NDEBUG
- m_nCount = 1;
- #endif
- }
- // Overloaded comma operator
- Initializer & operator,(T val)
- {
- #ifndef NDEBUG
- // Debug assert for array overrun
- assert(m_nCount < ArraySize);
- ++m_nCount;
- #endif
- // store value in pointer to array, then
- // increment pointer to array to next location
- *m_ptr++ = val;
- return *this;
- }
- private:
- // Pointer to array location
- T * m_ptr;
- #ifndef NDEBUG
- int m_nCount;
- #endif
- };