类模板中重载下标运算符的问题
我按书上的套路写的程序,一遇到放进模版类中就糊涂了。请高手帮我看看程序。#include<iostream>
using namespace std;
template<typename T>
class Vec3
{
private:
T x;
T y;
T z;
T length;
T* size;
public:
T GetX(){return x;}
T GetY(){return y;}
T GetZ(){return z;}
Vec3(T a,T b,T c):x(a),y(b),z(c){}
Vec3(){}
Vec3(T i){length=i;size=new T[length];}
~Vec3(){delete size;}
T GetLength(){return length;}
T& operator[](T i)
{
static T ch;
if(i>=0&&i<length)
return size[i];
else
{
cout<<"超出范围。";
return ch;
}
}
const T& operator[](T i) const
{
if(i>=0&&i<length)
return size[i];
else
{
cout<<"超出范围。"
return size[length]-1;
}
}
//流输出
//友可访问私有成员
friend ostream& operator<<(ostream& out,Vec3<T>& r)
{
out<<"("<<r.GetX()<<","<<r.GetY()<<","<<r.GetZ()<<")";
return out;
}
};
int main()
{
Vec3<double>a(1,2,3);
Vec3<int>e(3);
for(int i=0;i<=2;)
cout<<(e[1]=a[1])<<endl;
return 0;
}