(3)友元函数提供了一种非成员函数访问私有数据成员的途径,模板类使类中的数据成员的类型变得灵活,这两种技术可以结合起来用。要求在前面方案的基础上支持用友员函数实现的加法。用于测试的main()函数如下:
#include <iostream>
using namespace std;
template <class numtype1> //类声明前加模板的声明
class Complex
{
public:
Complex() //定义构造函数
{
real=0;
imag=0;
}
Complex(numtype1 r,numtype1 i) //构造函数重载
{
real=r;
imag=i;
}
Complex complex_add(const Complex &c2); //c2为常对象使其值不随意被改变
template<class numtype2> friend Complex<numtype2> add_complex(const Complex<numtype2 > &c1, const Complex<numtype2 > &c2); //利用了模板的外部函数要
//作为友元函数,注意声明方式:类声明中也必须给出模板声明。这一行程序可以在CodeBlocks中调试通过,将T2换成T1,VS2008也接受
void display( ); //声明输出函数
private:
numtype1 real;
numtype1 imag;
};
template <class numtype1> //每一个成员函数的定义前,必须要声明类模板
Complex<numtype1> Complex<numtype1>::complex_add(const Complex<numtype1> &c2) //使用了模板的类,将不再独立使用,其类名的完整表示为“类模板名<虚拟类型参数>”
{
Complex<numtype1> c; //凡用到类名处也用“类模板名<虚拟类型参数>”形式;本题中求两个复数的和,自然要产生一个新的复数对象
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
template<class numtype2>
Complex<numtype2> add_complex(const Complex<numtype2> &c1, const Complex<numtype2> &c2)
{
Complex<numtype2> c;
c.real=c1.real+c2.real;
c.imag=c1.imag+c2.imag;
return c;
}
template<class numtype1>
void Complex<numtype1>::display( )
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main( )
{
Complex<int> c1(3,4),c2(5,-10),c3;
c3=c1.complex_add(c2); //调用成员函数支持加法运算,有一个形参
cout<<"c1+c2=";
c3.display( );
Complex<double> c4(3.1,4.4),c5(5.34,-10.21),c6;
c6=c4.complex_add(c5); //调用成员函数支持加法运算,有一个形参
cout<<"c4+c5=";
c6.display( );
Complex<int> c7;
c7=add_complex(c1,c2); //调用友员函数支持加法运算,有两个形参
cout<<"c1+c2=";
c7.display( );
Complex<double> c8;
c8=add_complex(c4,c5); //调用友员函数支持加法运算,有两个形参
cout<<"c4+c5=";
c8.display( );
return 0;
}
总结:应用友元函数需要声明另一个模板,但是是否可以应用原来的模板呢?