cocos3 singleton

时间:2023-03-09 02:20:21
cocos3 singleton
class TestSingleton : public CCLayer
{
public:
static TestSingleton* getInstance();//创建一个全局访问点,例如我们常用的Director::getInstance()就是这样一个单例
void test();//测试单例时用到。 private:
static TestSingleton* m_singleton; };
//
// Global.cpp
// terr
//
// Created by sun on 15/11/8.
//
// #include "Global.h" TestSingleton* TestSingleton::m_singleton = NULL;//m_singleton指向空指针 TestSingleton* TestSingleton::getInstance()
{
//判断m_singleton是否已被实例化, 如果没有,将之实例
if(NULL == m_singleton)
{
m_singleton = new TestSingleton();//
}
return m_singleton;
} void TestSingleton::test()
{
CCLOG("test testSingleton success~~");
}