到后面学习开始变懒了 从代码就看出来了
#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
本文介绍了一个使用模板类实现的动态数组,并演示了如何通过运算符重载来简化对该数组的操作。此外,还展示了一个自定义类如何实现转换运算符及赋值运算符重载,以及如何自定义流插入运算符以支持类的输出。

被折叠的 条评论
为什么被折叠?



