Singleton -- template

1260 ワード

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;

}