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~~");
}
posted @ 2015-11-08 10:08  yufenghou  阅读(121)  评论(0编辑  收藏  举报