模板_静态函数_初始化

#include <iostream>
#include <map>
using namespace std;

template<typename T>
class S
{
public:
    S() {}
    static T* t;
    static void gen()
    {
        static T p;
        t = &p;
/*        t = new T();*/
    }
};

class A : public S<A> { public: char carr[1024]; };
A* A::t = NULL;

class B : public S<B> { public: char carr[2048]; };
B* B::t = NULL;

typedef void (*func)();

//////////////////////////////////////////////////////////////////////////

void main()
{
    map<int, func> con;
    con.insert(pair<int, func>(1, A::gen));
    con.insert(pair<int, func>(2, B::gen));

    cout<<A::t<<endl;
    cout<<B::t<<endl;

    con[1]();
    con[2]();

    cout<<A::t<<endl;
    cout<<B::t<<endl;

    con[1]();
    con[2]();

    cout<<A::t<<endl;
    cout<<B::t<<endl;

    system("pause");
}

 

posted on 2013-08-19 07:06  shizuka  阅读(295)  评论(0编辑  收藏  举报

导航