1 问题
实现c++的单例模式,这里测试分别写了通过智能指针返回对象和普通返回指针
2 代码测试
include <iostream>
#include <mutex>
#include <memory>
using namespace std;
class Single
{
public:
static Single& getInstance()
{
std::mutex mt;
if (instance.get() == NULL) {
mt.lock();
if (instance.get() == NULL) {
instance.reset(new Single());
}
mt.unlock();
}
return *instance;
}
private:
Single(){}
~Single(){}
static std::auto_ptr<Single> instance;
friend class std::auto_ptr<Single>;
Single(const Single&);
Single& operator= (const Single&);
};
std::auto_ptr<Single&g