C++单例

template<class T>
class Singleton
{
public:
    using object_type = T;
    struct object_creator
    {
        object_creator()
        {
            Singleton<T>::instance();
        }
    };
 
    static object_creator creator_object;
public:
    static object_type* instance()
    {
        static object_type _instance;
        return(&_instance);
    }
};
template<typename T> typename Singleton<T>::object_creator Singleton<T>::creator_object;

猜你喜欢

转载自blog.csdn.net/qq123hongli/article/details/79831461