template<class T>
xxxx function(){
}
===>>>
template<class T> xxxx function(){
}
template <class T>
class A
{
}
class B : pubilc class<int> A
{
}
template<class T>
class B : pubilc class<T> A
{
}
类函数模板是怎么实现的? 二次编译,类中的模板成员函数,在调用的过程中,才生成对应的函数。如果是int类型的成员函数,就生成int类型的。
类模板内的函数在类外实现的。主要问题,产生在的friend函数。
#include <iostream>
using namespace std;
template <class T> class Person;
template <class T> void print_t(Person<T> &s);
template<class T>
class Person
{
private:
T mh;
T mw;
public:
Person(T h,T w):mh(h),mw(w){};
friend void print_t<T>(Person<T> &s);
void show();
~Person(){};
};
template<class T>
void Person<T>::show(){
cout << this->mh << ","<< this->mw <<endl;
}
template<class T>
void print_t(Person<T> &s){
cout << s.mh << " ss," << s.mw << endl;
}
int main(){
Person<int> s(90,88);
s.show();
print_t(s);
return 0;
}