#ifndef _array_h_
#define _array_h_
/*
* implement an array class template, on the stack
*why
*2016/9/5
*/
Template
< typename T, int N >
Class Array
{
Private
T M_array[n];
Public
int length (); Get array length
BOOL Set_array (T value, int index); Set the contents of an array element
BOOL Get_array (t& value, int index); //Get array element contents
t& operator [] (int index); //overloaded [] operator for easy operation of array objects
T operator [] (int index) const; //If the user defines a const array object, then accessing the array element requires a const-decorated member function
Virtual ~array (); //destructors are best defined as virtual functions, allowing classes that inherit this class to override this destructor. Of course this is not vitrual, because if the constructor is private rather than protected,
Just don't want this class to be inherited, so you can not vitrual, but if the constructor is protected, the explanation is to be inherited, if you want to be inherited, then the destructor is better virtual
};
Template
< typename T, int N >
int array<t, N>::length ()
{
return N;
}
Template
< typename T, int N >
BOOL Array<t, N>::set_array (T value, int index)
{
BOOL ret = (0 <= index) && (Index < N);
if (ret)
{
M_array[index] = value;
}
return ret;
}
Template
< typename T, int N >
BOOL Array<t, N>::get_array (t& value, int index)
{
BOOL ret = (0 <= index) && (Index < N);
if (ret)
{
Value = M_array[index];
}
return ret;
}
Template
< typename T, int N >
t& array<t, n>::operator[] (int index)
{
return M_array[index];
}
Template
< typename T, int N >
T array<t, n>::operator[] (int index) const
{
return M_array[index];
}
Template
< typename T, int N >
Array<t, N>::~array ()
{
}
#endif
C + + Array class template (Stack memory)