游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2210|回复: 7

有关一个单态模板的问题。

[复制链接]

18

主题

631

帖子

660

积分

高级会员

Rank: 4

积分
660
发表于 2005-10-24 15:44:00 | 显示全部楼层 |阅读模式
template <typename TClass>
class c_Singleton
{
private:
        static TClass *m_pSingleton;
public:
        c_Singleton();
        virtual ~c_Singleton();
        static TClass& GetSingleton();
        static TClass* GetSingletonPtr();
};

//initialise pointer
template <typename TClass>
TClass *c_Singleton<TClass>::m_pSingleton = NULL;                               

//On construction of a derived class, update Singleton pointer to point to new object,
//and assert if there's more than one created
template <typename TClass>
c_Singleton<TClass>::c_Singleton()
{
        DebugCriticalAssert( !m_pSingleton );
        int offset = (int)(TClass*)(0x1)-(int)(c_Singleton<TClass>*)(TClass*)(0x1);
        m_pSingleton = (TClass*)((int)this + offset);
}

template <typename TClass>
c_Singleton<TClass>::~c_Singleton()
{
        DebugAssert( m_pSingleton );
        m_pSingleton = NULL;       
}

template <typename TClass>
TClass &c_Singleton<TClass>::GetSingleton() { return *m_pSingleton; }

template <typename TClass>
TClass *c_Singleton<TClass>::GetSingletonPtr() { return m_pSingleton; }

这里面的构造函数怎么看起来怪怪的。
在使用的时候是这样使用的:

class c_Render : public c_Singleton<c_Render>
{
        friend class c_RenderStack;
public:
        c_Render();
        virtual ~c_Render();



};

我发现如果多声明对象的话,m_pSingleton的指针实际是变化的,后产生的对象指针覆盖前次创建的对象的。

谁看明白这个模板给指点一下。

8

主题

239

帖子

239

积分

中级会员

Rank: 3Rank: 3

积分
239
发表于 2005-10-24 20:46:00 | 显示全部楼层

Re:有关一个单态模板的问题。

//On construction of a derived class, update Singleton pointer to point to new object,
//and assert if there's more than one created

看着是比较费劲,没看出来为什么只能创建一个, 按他的注解,如果more than one created就报错了。DebugCriticalAssert( !m_pSingleton );好想控制只能一个. DebugCriticalAssert这个函数到release模式还能管用吗?

36

主题

1047

帖子

1147

积分

金牌会员

Rank: 6Rank: 6

积分
1147
发表于 2005-10-25 11:36:00 | 显示全部楼层

Re:有关一个单态模板的问题。

没必要这么写。

121

主题

2029

帖子

2034

积分

金牌会员

Rank: 6Rank: 6

积分
2034
QQ
发表于 2005-10-25 16:28:00 | 显示全部楼层

Re:有关一个单态模板的问题。

int offset = (int)(TClass*)(0x1)-(int)(c_Singleton<TClass>*)(TClass*)(0x1);
计算偏移量,就是说可以得出正确的对象地址。

18

主题

631

帖子

660

积分

高级会员

Rank: 4

积分
660
 楼主| 发表于 2005-10-26 08:43:00 | 显示全部楼层

Re:有关一个单态模板的问题。

老外的解释:

The constructor is from an article on Singletons in Game Programming
Gems.
The purpose of the offset is to allow the singleton class to be used
with
polymorphic classes, although looking at it now I don't understand what
bug
this would fix! It's certainly safe to remove the offset calculation
for
DXQ3.

The rest of the singleton constructor saves the pointer of the first
object
of that type (hence using the template) to a static variable.
Subsequent
constructions of the object will assert.

5

主题

217

帖子

222

积分

中级会员

Rank: 3Rank: 3

积分
222
发表于 2005-10-26 18:11:00 | 显示全部楼层

Re:有关一个单态模板的问题。

脸红中....
一种单件模式的实现....

23

主题

515

帖子

552

积分

高级会员

Rank: 4

积分
552
发表于 2005-10-26 18:19:00 | 显示全部楼层

Re:有关一个单态模板的问题。

不明白为什么会把代码写的这么难看懂?
loki里面有一个代码可读性和安全性都很好的单件实现,建议看看那个。

18

主题

971

帖子

982

积分

高级会员

Rank: 4

积分
982
发表于 2005-10-26 22:33:00 | 显示全部楼层

Re:有关一个单态模板的问题。

这个单件和lloki的不同,是为了实现后面创建的对象覆盖前面对象而写的。
所以loki代替不了/.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-1-22 10:01

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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