单例模式与auto_ptr

#include<iostream>
#include<memory> //!!!!!!!!!!!!!!需要包含的头文件
using namespace std;
class Singleton
{
public:
    static Singleton* GetInstance()
    {
        if(instance_.get() == NULL)
        {
            instance_ = auto_ptr<Singleton>(new Singleton); //!!!!!!!!!!!!!!!!!!!!
        }
        return instance_.get();
    }
    ~Singleton()
    {
        cout << "~Singleton ..."<<endl;
    }
private:
    Singleton()
    {
        cout<< "Singleton ..."<<endl;
    }
    Singleton(const Singleton& other);
    Singleton& operator=(const Singleton& other);
    static auto_ptr<Singleton> instance_;  //!!!!!!!!!!!!!!!!!!auto_ptr
};

auto_ptr<Singleton> Singleton::instance_; //!!!!!!!!!!!!!!!!!
int main(void)
{
    Singleton* s1 = Singleton::GetInstance();
    Singleton* s2 = Singleton::GetInstance();
    return 0;
}

你可能感兴趣的:(单例模式与auto_ptr)