C++テンプレート簡単学習
959 ワード
後で勉強して怠け者になってコードから見えました
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
template <class T,int size=10>
class A
{
T o[size];
public:
int length()const { return size; }
T &operator[](int i);
};
template < class T,int size>
T&A<T, size>::operator[](int i)
{
if (i >= 0 && i < size)
return o[i];
}
class N
{
private:
float f;
public:
operator float()const
{
return f;
}
N &operator=(const N &c)
{
f = c.f;
return *this;
}
friend std::ostream &operator<< (std::ostream &os, const N&n)
{
return os << n.f;
}
N(float ff=0.0f) :f(ff){}
};
template<class T,int size=20>
class H
{
A <T,size> *np;
public:
H() :np(NULL){}
T &operator[](int i)
{
if (!np)
{
np = new A<T, size>;
}
return np->operator[](i);
}
int length()const { return size; }
~H()
{
delete np;
}
};
#endif