1. #include<iostream>
    2. using namespace std;
    3. #include<string.h>
    4. //类模板中成员函数的创建时机
    5. //类模板中成员函数在调用时才去创建
    6. class person1 {
    7. public:
    8. void show1() {
    9. cout << "person1 show1" << endl;
    10. }
    11. };
    12. class person2 {
    13. public:
    14. void show2() {
    15. cout << "person2 show2" << endl;
    16. }
    17. };
    18. template<class T>
    19. class myclass {
    20. public:
    21. T obj;
    22. void func1() {
    23. obj.show1();
    24. }
    25. void func2() {
    26. obj.show2();
    27. }
    28. };
    29. //类模板中成员函数在调用时才去创建
    30. void test01() {
    31. myclass<person1>m;
    32. m.func1();
    33. //m.func2();//不可调用
    34. }
    35. int main() {
    36. test01();
    37. return 0;
    38. }

    类模板中成员函数在调用时才去创建