帮我看看单例模式有啥问题?

ybxuwei ybxuwei 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include <memory>
#include <iostream>

using namespace std;

template<class T>
class Singleton
{
public:
static T *Instance()
{
if(instance_.get() == NULL)
instance_.reset(new T);
return instance_.get();
}
virtual ~Singleton(){}
protected:
Singleton(){}
private:
static std::auto_ptr<T> instance_;
};

template<class T>
std::auto_ptr<T> Singleton<T>::instance_;

class T : protected Singleton<T>
{
public:
void print(){cout << "this is a stest" << endl; }
};

int _tmain(int argc, _TCHAR* argv[])
{
T t; // 这样竟然能运行通过
t.print();
// T::Instance()->print();
return 0;
}
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
ybxuwei
C++ 语言 65,214 成员 250,514 内容