一、CCCopying(拷贝类)
Cocos2d-x的根类是CCCopying(类似CCTouchDelegate这样的类也没有父类,但是显然没有CCCopying“枝繁叶茂”),它的源码是一个只包含一个方法的类:
class CC_DLL CCCopying
{ public: virtual CCObject* copyWithZone(CCZone* pZone); };
这是个虚函数,而CCCopying又是根类,因此几乎所有类都可以覆盖这个方法。考虑到写这个笔记的目的是尽可能快地掌握Cocos2d-x引擎,所以关于拷贝的内容暂时先放在一边,等以后学到中高阶后再回来研究。
二、CCObject
头文件源码如下:
class CC_DLL CCCopying { class CC_DLL CCObject : public CCCopying { public: // object id,CCScriptSupport need public m_uID unsigned int m_uID; // Lua reference id int m_nLuaID; protected: // count of references unsigned int m_uReference; // count of autorelease unsigned int m_uAutoReleaseCount; public: CCObject(@H_502_52@void); virtual ~CCObject(@H_502_52@void); void release(@H_502_52@void); void retain(@H_502_52@void); CCObject* autorelease(@H_502_52@void); CCObject* copy(@H_502_52@void); bool isSingleReference(@H_502_52@void) const; unsigned int retainCount(@H_502_52@void) const; virtual bool isEqual(@H_502_52@const CCObject* @H_502_52@pObject); virtual void acceptVisitor(CCDataVisitor &@H_502_52@visitor); virtual void update(@H_502_52@float @H_502_52@dt) {CC_UNUSED_PARAM(@H_502_52@dt);}; friend class CCAutoreleasePool; }; //定义定时器访问类成员函数 typedef void (CCObject::*SEL_SCHEDULE)(@H_502_52@float); //定义普通回调类成员函数 typedef void (CCObject::*SEL_CallFunc)(); typedef void (CCObject::*SEL_CallFuncN)(CCNode*); typedef void (CCObject::*SEL_CallFuncND)(CCNode*,@H_502_52@void*); typedef void (CCObject::*SEL_CallFuncO)(CCObject*); typedef void (CCObject::*SEL_MenuHandler)(CCObject*); typedef void (CCObject::*SEL_EventHandler)(CCEvent*); typedef int (CCObject::*SEL_Compare)(CCObject*); #define schedule_selector(@H_502_52@_SELECTOR) (SEL_SCHEDULE)(&@H_502_52@_SELECTOR) #define callfunc_selector(@H_502_52@_SELECTOR) (SEL_CallFunc)(&@H_502_52@_SELECTOR) #define callfuncN_selector(@H_502_52@_SELECTOR) (SEL_CallFuncN)(&@H_502_52@_SELECTOR) #define callfuncND_selector(@H_502_52@_SELECTOR) (SEL_CallFuncND)(&@H_502_52@_SELECTOR) #define callfuncO_selector(@H_502_52@_SELECTOR) (SEL_CallFuncO)(&@H_502_52@_SELECTOR) #define menu_selector(@H_502_52@_SELECTOR) (SEL_MenuHandler)(&@H_502_52@_SELECTOR) #define event_selector(@H_502_52@_SELECTOR) (SEL_EventHandler)(&@H_502_52@_SELECTOR) #define compare_selector(@H_502_52@_SELECTOR) (SEL_Compare)(&@H_502_52@_SELECTOR) };
详细分析可以看红孩儿的博客:http://www.jb51.cc/article/p-nxwwxhyl-vd.html 。分析得非常详细,几乎每行代码都有注释,但是该不懂的还是不懂。然而没关系,会用就行。还是那句话:暂时先放在一边,等以后学到中高阶后再回来研究。
虽然没看懂,但是很显然CCObject类主要和内存管理(引用计数)有关,还定义了一些方便常用的方法。知道这两点就够了。
三、总结 1、知道了拷贝方法来源于CCCopying 2、知道了内存管理机制于CCObject类中实现 3、知道了常用的schedule_selector等宏于CCObject类中定义并实现