//
模板友元.cpp : 定义控制台应用程序的入口点。
//
#include " stdafx.h "
#include < iostream >
using namespace std;
template < class T >
class Box;
template < class T >
Box < T > operator + ( const Box < T > & , const Box < T > & );
template < class T >
Box < T > operator + ( const Box < T > & , const T & );
template < class T >
Box < T > operator + ( const T & , const Box < T > & );
template < class T >
ostream & operator << (ostream & , const Box < T > & );
template < class T >
class Box ... {
public:
Box(); //{
//tt = 0;
//}
Box(const T &);
void f();
void f(T &);
static void sta();
friend Box operator+ <T> (const Box<T> &, const Box<T> &);//要带<T>或者<>表示这个是一个模板
friend Box operator+ <> (const Box<T> &, const T &);
friend Box operator+ <> (const T &, const Box<T> &);
friend ostream& operator<< <T> (ostream &, const Box<T> &);
private:
T tt;
} ;
template < class T >
void Box < T > ::sta() ... {
T a = (T)100;
cout << a << "This is a static function!" <<endl;
}
template < class T >
Box < T > ::Box < T > () ... { //构造函数后面可以带<T>,不过编译会报警的
//Box<T>::类的成员函数的使用要用类作用域解析符号,但是对模板而言要使用模板形式的
tt = 0;
}
template < class T >
Box < T > ::Box( const T & newt) ... {
tt = newt;
}
template < class T >
void Box < T > ::f() ... { //但是类的非构造函数如果使用了<T>,
//即:void Box<T>::f<T>(),就不对了,因为类作用域解析符号已经足够体现 T的意义
//静态函数也不例外,可以拿上面的sta函数试试
T t = 100;
tt = t;
cout << tt << endl;
}
template < class T >
void Box < T > ::f(T & a) ... {
T t = 100;
tt = t;
cout << tt << endl;
}
template < class T >
Box < T > operator + < T > ( const Box < T > & a, const T & b) ... { //友元在类声明里面要带<T> 或<>在函数名后表示这个是一个模板
//而在外部定义时可以不带的
return Box<T>(a.tt + b);
}
template < class T >
Box < T > operator + ( const T & a, const Box < T > & b) ... {
return b+a;
}
template < class T >
Box < T > operator + ( const Box < T > & a, const Box < T > & b) ... {
return Box<T> (a.tt + b.tt);
}
template < class T >
ostream & operator << (ostream & os, const Box < T > & a) ... {
return os << a.tt << endl;
}
int _tmain( int argc, _TCHAR * argv[])
... {
Box<int> a(10);
Box<int> b(5);
Box<int> c;
c = 100 + b + 10;
cout << a << endl
<< b << endl
<< c << endl;
//c.f<int>();
c.f();
Box<int>::sta();
return 0;
}
//
#include " stdafx.h "
#include < iostream >
using namespace std;
template < class T >
class Box;
template < class T >
Box < T > operator + ( const Box < T > & , const Box < T > & );
template < class T >
Box < T > operator + ( const Box < T > & , const T & );
template < class T >
Box < T > operator + ( const T & , const Box < T > & );
template < class T >
ostream & operator << (ostream & , const Box < T > & );
template < class T >
class Box ... {
public:
Box(); //{
//tt = 0;
//}
Box(const T &);
void f();
void f(T &);
static void sta();
friend Box operator+ <T> (const Box<T> &, const Box<T> &);//要带<T>或者<>表示这个是一个模板
friend Box operator+ <> (const Box<T> &, const T &);
friend Box operator+ <> (const T &, const Box<T> &);
friend ostream& operator<< <T> (ostream &, const Box<T> &);
private:
T tt;
} ;
template < class T >
void Box < T > ::sta() ... {
T a = (T)100;
cout << a << "This is a static function!" <<endl;
}
template < class T >
Box < T > ::Box < T > () ... { //构造函数后面可以带<T>,不过编译会报警的
//Box<T>::类的成员函数的使用要用类作用域解析符号,但是对模板而言要使用模板形式的
tt = 0;
}
template < class T >
Box < T > ::Box( const T & newt) ... {
tt = newt;
}
template < class T >
void Box < T > ::f() ... { //但是类的非构造函数如果使用了<T>,
//即:void Box<T>::f<T>(),就不对了,因为类作用域解析符号已经足够体现 T的意义
//静态函数也不例外,可以拿上面的sta函数试试
T t = 100;
tt = t;
cout << tt << endl;
}
template < class T >
void Box < T > ::f(T & a) ... {
T t = 100;
tt = t;
cout << tt << endl;
}
template < class T >
Box < T > operator + < T > ( const Box < T > & a, const T & b) ... { //友元在类声明里面要带<T> 或<>在函数名后表示这个是一个模板
//而在外部定义时可以不带的
return Box<T>(a.tt + b);
}
template < class T >
Box < T > operator + ( const T & a, const Box < T > & b) ... {
return b+a;
}
template < class T >
Box < T > operator + ( const Box < T > & a, const Box < T > & b) ... {
return Box<T> (a.tt + b.tt);
}
template < class T >
ostream & operator << (ostream & os, const Box < T > & a) ... {
return os << a.tt << endl;
}
int _tmain( int argc, _TCHAR * argv[])
... {
Box<int> a(10);
Box<int> b(5);
Box<int> c;
c = 100 + b + 10;
cout << a << endl
<< b << endl
<< c << endl;
//c.f<int>();
c.f();
Box<int>::sta();
return 0;
}