1 #include <iostream> 2 using namespace std; 3 4 class myclass 5 { 6 public: 7 //函数模板,不调用不编译 8 template <class T> 9 T add(T a, T b) 10 { 11 return a + b; 12 } 13 14 template <class T> 15 static T sub(T a, T b) 16 { 17 return a - b; 18 } 19 }; 20 21 22 void main() 23 { 24 myclass *p = nullptr; 25 int num = p->add(1, 4); 26 //cout << num << endl; 27 //或者强行指定 28 int num2 = p->add<int>(4, 5); 29 30 int num3 = myclass::sub(9 ,3); 31 cin.get(); 32 }