友元 + 模板

#include <stdio.h>
#include <iostream>
using namespace std;

template <typename T> class Base;

template <typename T>
ostream & operator<<( ostream &out2 , Base<T> & base );

template <typename T>
class Base
{
private:
	T is_private;

public:
	int is_public;

	Base(T a,int b);
	~Base(){};

	void print()
	{
		cout<<"is_private "<<is_private<<endl;
		cout<<"is_public "<<is_public<<endl;
	}

	Base<T> operator+(int a);
	Base<T> operator=(Base<T> base);

	friend ostream & operator<< <T> ( ostream &out2 , Base<T> & base );

};

template <typename T>
Base<T>::Base(T a,int b)
{
	is_private = a;
	is_public = b;
}

template <typename T>
Base<T> Base<T>::operator+(int a)
{
	this->is_private = 'o';
	this->is_public = this->is_public + a;
	return (*this);
}

template <typename T>
Base<T> Base<T>::operator=(Base<T> base)
{
	this->is_private = base.is_private;
	this->is_public = base.is_public;
	return (*this);
}

template <typename T>
T mymax(const T a, const T b)
{
	return a > b ? a : b;
}

template <typename T>
ostream & operator<<( ostream &out2 , Base<T> & base )
{
	out2 <<"overload << is:" << base.is_public << endl;
	return out2;
}

int main()
{
	int a, b;
	scanf("%d",&a);
	scanf("%d",&b);
	printf("%d\n",mymax(a,b));

	Base<char> what('a',3);
	what.print();

	Base<char> what2('b',4);
	cout<<what2<<endl;
	what2 = what;
	cout<<what2<<endl;
	what2 = what+3;
	cout<<what2<<endl;

	return 0;
}
在模板中使用友元 比较麻烦。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值