如何实现模板类? 如何实现模板类? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个也许对你有用#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); //ÇóÄ£ 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();//ÇóÄ£ cout<<f; c=a*b; return 0; } 也许Delphi根本不支持类模板! DELPHI是不支持的!这是常识,当然以后会不会支持就很说 Delphi在语言机制上是不支持类模板的,但是,Delphi提供了一个强大的包容类库Contnrs.pas.它可以模拟绝大部份类模板的功能,另外,classes.pas里的Tlist也是一个很好的包容类. 加班暂告一段落,散分 delphi出错:dataset not in edit or insert mode怎么办? 单击DBGrid的每一行数据都会弹出一个错误 关于Delphi深度探索-活动目录开发 如何解决 谁能告诉我这个Error是什么意思,可能是什么地方有问题: 求《Delphi 7项目开发实践》每个项目实例的简要介绍 怎样学delphi? 怎样做一个启动封面 用copyfile(Pchar(),Pchar(),false)出现"Invalid typecast"??? 在delphi中如何调用ie浏览器打开指定的文件。 DBGrid控件的使用
#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);
//ÇóÄ£
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();//ÇóÄ£
cout<<f;
c=a*b;
return 0;
}
它可以模拟绝大部份类模板的功能,另外,classes.pas里的Tlist也是一个很好的包容类.