singleton.h

 

#ifndef __SINGLETON__H_

#define __SINGLETON__H_

 

#include "cocos2d.h"

USING_NS_CC;           // using namespace cocos2d

 

class singleton

{

 

      public : 

            static singleton* getInstance();

 

      private : 

            static singleton* instance;

 

}

 

#endif // !__SINGLETON__H_

 

 

 

singleton.cpp

 

#include "singleton.h"

 

singleton* singleton::instance;

 

singleton* singleton::getInstance()

{

      if(!instance)

      {

            instance = new singleton;

      }

 

      return instance;

}

 

 

ヘッダファイルで定義したstatic変数は必ずcppで再定義しなければエラーが発生しまいますのでご注意ください。