// 模板与友元
#include "stdafx.h"
#include<iostream>
using namespace std;
// 要进行前向声明
template<class T> class Friendly;
// 要进行前向声明
template<class T> void f(const Friendly<T>& );
template<class T>
class Friendly
{
T t;
public:
Friendly(const T& theT): t(theT) { }
// 友元函数 用<>表示其函数为友元的模板函数,不是友元的普通函数
friend void f<> (const Friendly<T>&);
void g() { f(*this); }
};
void h()
{
f(Friendly<int>(1));
}
template<class T>
void f(const Friendly<T>& fo)
{
std::cout<<fo.t<<std::endl;
}
int main()
{
h();
Friendly<int>(2).g();
system("pause");
return 0;
}
// 模板与友元2 提供一个简单的流来验证程序所做的工作
#include "stdafx.h"
#include<iostream>
using namespace std;
// Forward declarations 前向声明
template<class T> class Box;
template<class T>
Box<T> operator+(const Box<T>&, const Box<T>&);
template<class T>
ostream& operator<<(ostream&, const Box<T>&);
template<class T>
class Box
{
T t;
public:
Box(const T& theT): t(theT) { }
// 重载运算符的模板函数
friend Box operator+<>(const Box<T>&, const Box<T>&);
friend ostream& operator<< <>(ostream&, const Box<T>&);
};
template<class T>
Box<T> operator+(const Box<T>& b1, const Box<T>& b2)
{
return Box<T>(b1.t + b2.t);
}
template<class T>
ostream& operator<<(ostream& os, const Box<T>& b)
{
return os<<'['<<b.t<<']'<<std::endl;
}
int main()
{
Box<int> b1(1), b2(2);
cout<<b1+b2;
system("pause");
return 0;
}
// 以上代码有个缺点 无法提供隐式转换, 使用内部类,非模板的方法
// 使程序更短小,更强健
#include "stdafx.h"
#include<iostream>
using namespace std;
// Forward declarations 前向声明
template<class T>
class Box
{
T t;
public:
Box(const T& theT): t(theT) { }
// 重载运算符的模板函数
friend Box operator+(const Box<T>& b1, const Box<T>& b2)
{
return Box<T>(b1.t + b2.t);
}
friend ostream& operator<<(ostream& os, const Box<T>& b)
{
return os<<'['<<b.t<<']'<<std::endl;
}
};
int main()
{
Box<int> b1(1), b2(2);
cout<<b1+b2;
cout<<b1+3;
system("pause");
return 0;
}
#include "stdafx.h"
#include<iostream>
using namespace std;
// 要进行前向声明
template<class T> class Friendly;
// 要进行前向声明
template<class T> void f(const Friendly<T>& );
template<class T>
class Friendly
{
T t;
public:
Friendly(const T& theT): t(theT) { }
// 友元函数 用<>表示其函数为友元的模板函数,不是友元的普通函数
friend void f<> (const Friendly<T>&);
void g() { f(*this); }
};
void h()
{
f(Friendly<int>(1));
}
template<class T>
void f(const Friendly<T>& fo)
{
std::cout<<fo.t<<std::endl;
}
int main()
{
h();
Friendly<int>(2).g();
system("pause");
return 0;
}
// 模板与友元2 提供一个简单的流来验证程序所做的工作
#include "stdafx.h"
#include<iostream>
using namespace std;
// Forward declarations 前向声明
template<class T> class Box;
template<class T>
Box<T> operator+(const Box<T>&, const Box<T>&);
template<class T>
ostream& operator<<(ostream&, const Box<T>&);
template<class T>
class Box
{
T t;
public:
Box(const T& theT): t(theT) { }
// 重载运算符的模板函数
friend Box operator+<>(const Box<T>&, const Box<T>&);
friend ostream& operator<< <>(ostream&, const Box<T>&);
};
template<class T>
Box<T> operator+(const Box<T>& b1, const Box<T>& b2)
{
return Box<T>(b1.t + b2.t);
}
template<class T>
ostream& operator<<(ostream& os, const Box<T>& b)
{
return os<<'['<<b.t<<']'<<std::endl;
}
int main()
{
Box<int> b1(1), b2(2);
cout<<b1+b2;
system("pause");
return 0;
}
// 以上代码有个缺点 无法提供隐式转换, 使用内部类,非模板的方法
// 使程序更短小,更强健
#include "stdafx.h"
#include<iostream>
using namespace std;
// Forward declarations 前向声明
template<class T>
class Box
{
T t;
public:
Box(const T& theT): t(theT) { }
// 重载运算符的模板函数
friend Box operator+(const Box<T>& b1, const Box<T>& b2)
{
return Box<T>(b1.t + b2.t);
}
friend ostream& operator<<(ostream& os, const Box<T>& b)
{
return os<<'['<<b.t<<']'<<std::endl;
}
};
int main()
{
Box<int> b1(1), b2(2);
cout<<b1+b2;
cout<<b1+3;
system("pause");
return 0;
}