手撕代码 单例模式 饿汉和懒汉

class Lazy_Singleton {
private:
    Lazy_Singleton() {};
    ~Lazy_Singleton() {};
    Lazy_Singleton(const Lazy_Singleton&) {};
    Lazy_Singleton& operator = (const Lazy_Singleton&);
public:
    static Lazy_Singleton& getInstance() {
        static Lazy_Singleton instance;
        return instance;
    }
};

class Eager_Singleton {
private:
    static Eager_Singleton instance;
private:
    Eager_Singleton() {};
    ~Eager_Singleton() {};
    Eager_Singleton(const Eager_Singleton&) {};
    Eager_Singleton& operator = (const Eager_Singleton&);
public:
    static Eager_Singleton& getInstance() {
        return instance;
    }
};
Eager_Singleton Eager_Singleton::instance;

 

posted @ 2023-09-08 13:01  SuperTonyy  阅读(9)  评论(0)    收藏  举报