如何实现模板类?

解决方案 »

  1.   

    这个也许对你有用
    #include <iostream.h>
    #include <math.h>template <class T>
    class complex
    {
    public :
    T x,y;
    public :
    complex(T a=0,T b=0)
    {
    x=a;
    y=b;
    }
    friend complex<T>  operator*=(const complex<T> &a,const complex<T> &b);
    //&Ccedil;ó&Auml;&pound;
    T operator()()
    {
    T t;
    t=(T)sqrt(x*x+y*y);
    return t;
    } complex<T> Add(complex<T> a);
    complex<T> Sub(complex<T> a);
    };template <class T>
    complex<T>  operator*(const complex<T> &a,const complex<T> &b)
    {
    complex<T> t(0,0);
    t.x=a.x*b.x - a.y * b.y;
    t.y=a.x*b.y + a.y * b.x;
    return t;
    }
    template <class T>
    complex<T> complex<T>::Sub(complex<T> a)
    {
    complex<T> t;
    t.x=x-a.x;
    t.y=y-a.y;
    return t;
    }
    template <class T>
    complex<T> complex<T>::Add(complex<T> a)
    {
    complex<T> *t=new complex<T>;
    t->x=x +a.x;
    t->y=y +a.y;
    return *t;
    }
    int main()
    {
    complex<float> a(3,4),b(2,2);
    complex<float> c;
    c=a.Add(b);
    float f;
    f=a();//&Ccedil;ó&Auml;&pound;
    cout<<f;
    c=a*b;
    return 0;
    }
      

  2.   

    也许Delphi根本不支持类模板!
      

  3.   

    DELPHI是不支持的!这是常识,当然以后会不会支持就很说
      

  4.   

    Delphi在语言机制上是不支持类模板的,但是,Delphi提供了一个强大的包容类库Contnrs.pas.
    它可以模拟绝大部份类模板的功能,另外,classes.pas里的Tlist也是一个很好的包容类.