类模板

以代码的形式展现一下“类模板”的使用:

//.h文件
template<typename T>
class complex
{
public:
	complex(T r = 0, T i = 0)
	: re(r), im(i)
	{  }
	complex& operator += (const complex&);
	T real() const { return re; }
	T imag() const { return im; }
	
private:
	T re, im;
};
//使用
complex<double> c1(2,5, 3,8);
complex<int> c2(3, 4);
....

简化重复代码量。

使用注意:

在使用“类模板”的时候,需要明确指出所需要绑定的类的名称(编译器不能识别你所给的类型,只能通过使用者显式地指出)。

你可能感兴趣的:(C++相关,c++)