游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1714|回复: 0

设计模式---单件(C++版) 比较通用的单件模式(STL实现)

[复制链接]

4

主题

7

帖子

7

积分

新手上路

Rank: 1

积分
7
发表于 2010-8-30 14:48:00 | 显示全部楼层 |阅读模式
#pragma once
#include <memory>
using namespace std;
template<class _Tr,class _Ac>
class singleton
...{
public:
    singleton()
    ...{
        if(m_ptr.get() == NULL)
        ...{
            m_ptr.reset(new _Tr());
        }
    }
    singleton(const _Ac& args)
    ...{
        if(m_ptr.get() == NULL)
        ...{
            m_ptr.reset(new _Tr(args));
        }
    }
    _Tr* operator->()const
    ...{
        return m_ptr.get();
    }
    _Tr& operator*()const
    ...{
        return *m_ptr;
    }
public:
    ~singleton(void)
    ...{
    }
private:
    static auto_ptr<_Tr> m_ptr;
};

template<class _Tr,class _Ac>
auto_ptr<_Tr> singleton<_Tr,_Ac>::m_ptr;
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-8-18 20:16

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表