类模板:
#includeusing namespace std; template class test { public: static T i; static T g; void fun1() { cout << "fun()函数调用"< //在类外定义 void test ::fun() { cout << "fun1()在类模板外定义函数"< //在类模板外给成员变量赋值 T test ::i = 1,test ::g=10; int main() { test M_; //类模板创建对象 M_.fun1(); cout << "M_.i==" << M_.i << endl; cout << "M_.g==" << M_.g << endl; return 0; }
函数模板 :
#includeusing namespace std; template T fgf(T x, T y) { return x + y; } //函数模板重载 int fgf(int x, int y) { return x + y; } //函数模板调用 int main() { //函数模板调用: cout << fgf (10,10); return 0; }



