类模板遇上static

每一个具体的类的类型,是一个static。

 

定义一个模板类,不同类型的类使用的static不是同一个。

 

因为编译器自动帮我们写了不同的类型的类。

 

template<typename T>
class A
{
public:
    static int m_a;
protected:
private:
    T a1;

};

template<typename T>
int A<T>::m_a=0;


#include "iostream"
using namespace std;

void main()
{
    A<int> a1;
    A<int> a2;
    A<int> a3;

    A<char> b1;
    A<char> b2;
    A<char> b3;


    a1.m_a ++;
    b3.m_a = 199;
    cout<<a3.m_a<<endl;

    cout<<b2.m_a<<endl;


    system("pause");
}

 

posted @ 2016-11-15 17:40  ren_zhg1992  阅读(82)  评论(0)    收藏  举报