友元+テンプレート

1858 ワード

#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
",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; }
テンプレートで友元を使うのは面倒です.