天天看點

C++類模闆中成員函數建立時機

C++類模闆中成員函數建立時機

#include<iostream>
using namespace std;
//類模闆中成員函數建立時機
//1.普通類中的成員函數一開始就可以建立
//2.類模闆中成員函數在調用時才建立
class Person1
{
public:
       void showPerson1()
       {
              cout << "Person1 show" << endl;
       }
};
class Person2
{
public:
       void showPerson2()
       {
              cout << "Person2 show" << endl;
       }
};
template<class T>
class MyClass
{
public:
       T obj;
       //類模闆中的成員函數
       void func1()
       {
              obj.showPerson1();
       }
       void func2()
       {
              obj.showPerson2();
       }
};
void test01()
{
       MyClass<Person1>m;
       m.func1();
       //m.func2();
}
int main()
{
       test01();
       return 0;
}