Singleton pattern in C++

View Code
/*
refer:http://stackoverflow.com/questions/270947/can-any-one-provide-me-a-sample-of-singleton-in-c/271104#271104 
   */
#include <vector>
#include <string>
#include <iostream>
using namespace std;

class Sin
{
    public:
    static Sin& getInstance()
    {
        static Sin instance;
        
        return instance;
    }
    void print()
    {
        cout << val << endl;
    }

    private:
    static int val;
    Sin(){}
    Sin(const Sin& rhs){}
    Sin& operator = (const Sin& rhs){}
};

int Sin::val = 0xffffffff;

int main()
{
    Sin& s = Sin::getInstance();
    s.print();

    return 0;
}

 

posted @ 2013-04-01 21:21  strorehouse  阅读(172)  评论(0编辑  收藏  举报