singleton.h

来自「小型的3D游戏引擎」· C头文件 代码 · 共 36 行

H
36
字号
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
/********************************************************************
	created:		?
	author:			Scott Bilas, Game Programming Gems
	copyright:		Copyright 2003 (C) Scott Bilas
	description:	Singleton class
*********************************************************************/
//#include "assert.h"

template <typename T> class Singleton
{
	public:
		Singleton()
		{
			//assertex( !m__singleton_ && "Multiple singletons" );
			int offset = (int)(T *)1 - (int)(Singleton <T> *)(T *)1;
			m__singleton_ = (T *)((int)this + offset);
		}
		
		~Singleton()
		{
			//assertex( m__singleton_ );
			m__singleton_ = NULL;
		}
		
		static T & GetSingleton() { /*assertex( m__singleton_ );*/  return ( *m__singleton_ ); }
		static T * GetSingletonPtr() { return ( m__singleton_ ); }

	private:
		static T * m__singleton_;
};

template <typename T> T * Singleton <T>::m__singleton_ = 0; 

#endif /* _SINGLETON_H_ */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?