Design Pattern --- Singleton

// Singleton.h
class Singleton
{
private:
    Singleton() {}
    ~Singleton() {}
    Singleton(Singleton const &other);
    Singleton &operator=(Singleton const &other);
public:
    static Singleton &Instance() { return ms_singleton; }

    static Singleton ms_singleton;
};

// Singleton.cpp
// PUT IT in ".cpp" file.
Singleton Singleton::ms_singleton;

int main(int argc, char *argv[])
{
    Singleton &s1 = Singleton::Instance(),
              &s2 = Singleton::Instance();

    cout <<&s1 <<" : " <<&s2 <<endl;

    return 0;
}

posted @ 2013-01-22 11:39  walfud  阅读(77)  评论(0编辑  收藏  举报