学习Cocos2dx引擎之CCApplication

写在开头的话

本人工作是有5,6年了,但是C++也荒废了5.6年,这次重新拿起来忘记了好多东西.有什么地方说的不对的地方请各位高手指点一下.本人非常感谢.

CCApplication的来源

CCApplication继承自CCApplicationProtocol,这个我们先不管它,我们自己写个例子

1.新建CCApplication类

/ * CCApplication.h * * Created on: 2013-9-21 * Author:
sjbwybls
/

#ifndef CCAPPLICATION_H_

#define CCAPPLICATION_H_

#include

#include

namespace TestCocos2dx {

using namespace std;

class CCApplication { public: CCApplication(); virtual
~CCApplication(); int run(); static CCApplication *
sharedApplication() { cout«“return static app”«endl; return
sm_pSharedApplication; }

protected:

static CCApplication * sm_pSharedApplication;

};

} / namespace TestCocos2dx /

#endif / CCAPPLICATION_H_ /

/ * CCApplication.cpp * * Created on: 2013-9-21 * Author:
sjbwybls
/

#include “CCApplication.h”

namespace TestCocos2dx {

CCApplication *CCApplication::sm_pSharedApplication = 0;

CCApplication::CCApplication() { cout « “app ctor” « endl;
sm_pSharedApplication = this; }

CCApplication::~CCApplication() { cout « “app destroy” « endl;
sm_pSharedApplication = NULL; }

int CCApplication::run(){ cout « “run() function,
sm_pSharedApplication = " « sm_pSharedApplication « endl; return 0;

}

} / namespace TestCocos2dx /

  1. 新建CCDerivedCCApplication类继承自CCApplication

/ * CCDerivedCCApplication.h * * Created on: 2013-9-21 *
Author: sjbwybls
/

#ifndef CCDERIVEDCCAPPLICATION_H_

#define CCDERIVEDCCAPPLICATION_H_

#include

#include

#include “CCApplication.h”

namespace TestCocos2dx {

class CCDerivedCCApplication: public TestCocos2dx::CCApplication {
public: CCDerivedCCApplication(); virtual ~CCDerivedCCApplication();
};

} / namespace TestCocos2dx /
#endif / CCDERIVEDCCAPPLICATION_H_ /

/ * CCDerivedCCApplication.cpp * * Created on: 2013-9-21 *
Author: sjbwybls
/

#include “CCDerivedCCApplication.h”

namespace TestCocos2dx {

using namespace std;

CCDerivedCCApplication::CCDerivedCCApplication() {
cout«“DerivedCCApplication ctor”«endl;

}

CCDerivedCCApplication::~CCDerivedCCApplication() {
cout«“DerivedCCApplication destory”«endl; }

} / namespace TestCocos2dx /

3.最后建立main.cpp

/ * main.cpp * * Created on: 2013-9-21 * Author: sjbwybls
/

#include “CCApplication.h”

#include “CCDerivedCCApplication.h”

using namespace TestCocos2dx;

int main() {

CCApplication::sharedApplication()->run(); 
CCDerivedCCApplication app;
CCApplication::sharedApplication()->run(); 

}

4.输出结果
return static app
run() function, sm_pSharedApplication = 0
app ctor
DerivedCCApplication ctor
return static app
run() function, sm_pSharedApplication = 0x7fff56b50c20
DerivedCCApplication destory
app destroy

5.分析
main.cpp中的实例化了一个CCDerivedCCApplication的实例app。
调用流程:
返回静态父类实体并执行run函数

父类构造函数
子类构造函数
执行run函数
析构子类函数
析构父类函数

6.结论
先从C++的语法说明:
C++中静态函数不会触发构造函数
声明一个对象不需要new.即可触发构造函数(具体可参考)
父类跟子类的
构造函数调用关系为
先初始化父类构造函数再初始化子类构造函数
析构函数为
刚好相反
先调用子类析构函数再调用父类析构函数
CCApplication的用法
CCApplication app; //先调用Application必须先初始化Application,也可以从子类调用
CCApplication::sharedApplication()->run(); //最后执行run方法才能拿到初始化之后的Application,负责就只会等到空指针错误,这个也就是Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1)错误出现的原因吧,希望高手拍砖

本文启发自:初学试试看cocos2dx的TestCPP框架(1)

你可能感兴趣的:(C++,cocos2dx)