1 template<class T> 2 class pmy 3 { 4 public: 5 pmy() 6 { 7 8 } 9 pmy(T *t) 10 { 11 p = t; 12 } 13 ~pmy() 14 { 15 if (p != nullptr) 16 { 17 delete p; 18 } 19 } 20 //T operator *() 21 //{ 22 // return *p; 23 //} 24 25 private: 26 T *p = nullptr; 27 }; 28 29 class Test 30 { 31 public: 32 Test() 33 { 34 cout << "Test create" << endl; 35 } 36 ~Test() 37 { 38 cout << "Test delete" << endl; 39 } 40 }; 41 42 void run() 43 { 44 pmy<Test> p =new Test; //p是Test的一个指针.因为把Test替换成T. 45 } 46 47 void main() 48 { 49 run(); 50 cin.get(); 51 52 }