类模板下运算符重载的两种用法

1

template 
class Complex
{
	friend ostream &operator <<(ostream &out,const Complex &c);
private:
	T m_a;
	T m_b;
public:
	Complex(T a,T b);
	void print();
	
};

friend ostream &operator <<(ostream &out,const Complex &c);

这句的放在运算符<<后是因为该模板类中的成员变量都为T类;

2

template 
class Myarray
{
private:
	int m_len;
	T *m_data;
public:
	//Myarray();
	Myarray(int l);
	Myarray(const Myarray &a);
	~Myarray();
	template 
	friend ostream &operator <<(ostream &out,const Myarray &a);
	T &operator [](int index);
};

friend ostream &operator <<(ostream &out,const Myarray &a);

而此句的类模板中有一个成员变量是int型已确定,因此将放入参数Myarray后。

你可能感兴趣的:(C++)