#include "iostream"
template <typename type>
class myctn
{
public:
myctn( const myctn<type> & ); // 赋值构造
myctn( int size = defsize ); // 自定义容器大小
~myctn(){delete [] items;}
type& operator [] ( int ) ; // 赋值
type operator [] ( int ) const ; // 取数
private:
enum { defsize = 10 };
int ctnsize;
int ctnlength;
type* items;
};
template <typename type>
myctn<type>::myctn(int size):ctnsize(size)
{
items = new type [size];
ctnlength = 0;
}
template <typename type>
myctn<type>::myctn(const myctn<type> & ctn)
{
ctnsize = ctn.ctnsize;
items = new type [ctn.ctnsize];
ctnlength = ctn.ctnlength;
for ( int i=0; i<ctn.ctnlength; i++ )
items[i] = ctn.items[i];
}
template <typename type>
type& myctn<type>::operator [](int num)
{
if ( num >= ctnsize || num < 0)
{
std::cout<<num<<" it is out of limit !"<<std::endl;
std::exit (exit_failure);
}
return *(items+num);
}
template <typename type>
type myctn<type>::operator [](int num) const
{
if ( num >= ctnsize || num < 0 )
{
std::cout<<num<<" it is out of limit !"<<std::endl;
std::exit (exit_failure);
}
return *(items+num);
}
void main()
{
using std::cout;
myctn<int> aaa(5); // 构造通过不了。
}