Singleton template

template<CLASS T> class Singleton
{
public:
    static T& Instance()
    {
        static T theSingleInstance; //假?设?T有?一?个?protected默?认?构?造?函?数?
        return theSingleInstance;
    }
};

class OnlyOne : public Singleton<ONLYONE>
{
    friend class Singleton<ONLYONE>;
    int example_data;

    public:
    int GetExampleData() const {return example_data;}
    protected:
    OnlyOne(): example_data(42) {} // 默?认?构?造?函?数?
    OnlyOne(OnlyOne&) {}
};

int main( )
{
    cout << OnlyOne::Instance().GetExampleData()<< endl;
    return 0;
}
原文地址:https://www.cnblogs.com/wzh206/p/1726838.html