C++ Notes:Example - CheckedArray Alternate Style

CheckedArray Example 2

This shows another way to organize the header file for template classes. In Example - CheckedArray Template Class the constructor/method definitions were in the class. This style puts the method definitions after the class. Note that each method definition must then be preceded with both a template statement and the class qualifier.
//--- file generic/CheckedArray.h

#include <stdexcept>

/////////////////////////////////// class CheckedArray<T>
template<class T>
class CheckedArray {
    int size;  // maximum size
    T* a;      // pointer to new space
    CheckedArray<T>(int max);
    T& CheckedArray<T>::operator[](int index);
};//end class CheckedArray<T>

//================================= constructor
template<class T>
CheckedArray<T>::CheckedArray<T>(int max) {
    size = max;
    a = new T[size];
}//end constructor

//================================= operator[]
template<class T>
T& CheckedArray<T>::operator[](int index) {
    if (index < 0 || index >= size)
        throw out_of_range("CheckedArray");

    return a[index];
}//end operator[]
The calling program is the same as in Template Example 1.