Statement macros are commonly used. First, assertions. [Cpp] CCAssert (cond, msg); // The assertion expression cond is true. If not, the string msg information is displayed to traverse the macros of CCARRAY and CCDICTIONARY [cpp] CCArray * _ array; CCObject * _ object; // The Temporary Variable CCARRAY_FOREACH (_ array, _ object) used to traverse the array // forward traversal {// todo with _ object ....} CCARRAY_FOREACH_REVERSE (_ array, _ object) // reverse traversal {// todo with _ object ....} CCDictionary * _ dict; CCDictElement * _ elmt; // The Temporary Variable CCDICT_FOREACH (_ dict, _ elmt) for table traversal {// todo with elmt;} CCArr Both ay And CCDictionary do not implement templates. Forced conversion is required after the traversal elements are obtained. For example, the elements in the array are of the same type, for example, [cpp] CCArray * _ array; CCObject * _ object; // The Temporary Variable CCARRAY_FOREACH (_ array, _ object) used to traverse the array) // forward traversal {CCSprite * _ bullet = (CCSprite *) _ object; // todo with _ bullet ....} I always think that I have defined a CCObject * _ object because it is useless? And I am too lazy to write another sentence for forced conversion, can I? First, let's see how CCARRAY_FOREACH is defined. CCArray. h [cpp] # define CCARRAY_FOREACH (_ array __, _ object _) \ if (_ array _) & (_ array __) -> data-> num> 0) \ for (CCObject ** arr = (_ array _)-> data-> arr, ** end = (_ array _)-> data-> arr + (_ array _)-> data-> num-1; \ arr <= end & (_ object _) = * arr )! = NULL/* | true */); \ arr ++) when defining the type, you often need to define some getter setter, including CC_PROPERTY and CC_SYNTHESIZE brought by cocos2d from objc. [Cpp class Ship: public cocos2d: CCNode {// defines the m_energy variable of an int class. The access permission for this variable is protected. // The following method name is "Energy", which declares an int getEnergy () and a void setEnergy (int value) method. You need to define CC_PROPERTY (int, m_energy, Energy); // basically the same as above, but the get method is referenced, that is, an int & getEnergy (); CC_PROPERTY_PASS_BY_REF (int, m_energy, Energy) is declared ); // define variables, but only get methods are declared. The specific implementation must define CC_PROPERTY_READONLY (int, m_energy, Energy) in cpp; CC_PROPERTY_READONLY_PASS_BY_REF (int, m_energy, Energy ); // define variables, and directly define the default get/set method. Similarly, there are four categories of CC_SYNTHESIZE (cocos2d: CCObject *, m_weapon, Weapon); cosine (cocos2d: CCObject *, m_weapon, Weapon); cosine (cocos2d :: CCObject *, m_weapon, Weapon); invoke (cocos2d: CCObject *, m_weapon, Weapon); // call the release of the original m_weapon when setWeapon, and call the retain of the new value. Of course, exceptions (equal or NULL) have been ruled out ). CC_SYNTHESIZE_RETAIN (cocos2d: CCObject *, m_weapon, Weapon) ;}; note that 1. CC_PROPERTY is more suitable for quickly declaring a value attribute, while CC_SYNTHESIZE is more suitable for declaring an object. Because the default set provided by CC_SYNTHESIZE does not have any validity check, it is too impractical for value attributes. 2. All declarations of these methods are virtual. Even inline Methods declared as virtual do not generate inline functions. Therefore, their efficiency is not high, whether it is CC_PROPERTY or CC_SYNTHESIZE. 3. the get method of CC_PROPERTY does not declare the const modifier for the function body, which means that for the const object, and cannot call the get method declared by CC_PROPERTY (How do I think this is a cocos2d-x BUG ......). 4. After the CC_SYNTHESIZE method, directly declare the function or variable to public :...... Note: Well. Not easy to use? Skip to see the definition below and define one by yourself ...... If you are too lazy to read it, forget it. Then there is a quick CREATE_FUNC that automatically generates a default static create method. This facilitates [cpp] class Class: public cocos2d: CCNode {public: CREATE_FUNC (Class); // automatically generates a create static method without parameters, returns a Class * type pointer. The init and autorelease methods are automatically called.} // CREATE_FUNC (Class) is equivalent to the following static Class * create () {Class * pRet = new Class (); if (pRet & pRet-> init () {pRet-> autorelease (); return pRet ;}else {delete pRet; pRet = NULL; return NULL ;}} in addition, this is also the usage specification of the recommended C ++ constructor and init method. It is initialized immediately after the space is allocated, and the initialization result determines whether an available object can be returned. This should also be done when defining the create Method for a specific parameter. When it comes to initialization, we have to talk about the structure and some macros related to the structure. If I want to release a bunch of objects, I have to judge whether the object is NULL? Do you want to assign NULL values to the items after release? Programmers are too lazy to write so many lines of code ...... [Cpp] // the so-called safe logic is like this. First, check whether the pointer p is NULL. If it is not NULL, execute delete p or p-> release. CC_SAFE_DELETE (p); // when p is not NULL, delete p and assign p to NULL CC_SAFE_DELETE_ARRAY (p );//... delete [] p .. CC_SAFE_FREE (p );//... free p... CC_SAFE_RELEASE (p); // when p is not NULL, p-> release () CC_SAFE_RELEASE_NULL (p); // when p is not NULL, p-> release () and assign p to NULL CC_SAFE_RETAIN (p); // when p is not NULL, p-> retain () and two variables are exchanged by the way, you may like to be lazy, write a void swap (int & a, int & B) or something, then write a void swap (float & a, float & B) or something, and then write a void swap (string & a, str Ing & B) or something ...... I always feel that you are too lazy to be a library programmer ...... Here is a macro of CC_SWAP ...... [Cpp] CC_SWAP (x, y, type); // equivalent to the following {type temp = (x); x = y; y = temp ;} // at least the macro works normally when x and y are not expressions, and there is no need to worry about repeated temp variables or CC_BREAK_IF, which cocos2d library developers like to use, does this macro have any special meaning? Isn't it actually the if (?) of a row (???) Break ;? Well, it's ...... No difference. But you don't think CC_BREAK_IF (??? ); Is it lazy to be better than others? Now IDE can automatically tab out the macro! In addition, the following while (0) loop write can replace some if (???) Return false; yes! [Cpp] bool Class: init () {bool bRet = false; do {// do some initialization 1 CC_BREAK_IF (cond); // jumps out when the expression cond is true. // Do some more initialization bRet = true;} while (0); return bRet;} Every time you write one. h, cocos2d library programmers need to write a namespace cocos2d {...} you always need to use using namespace whenever you write a cpp ?.. They are too lazy to type these letters .. [Cpp] NS_CC_BEGIN // This is namespace cocos2d {NS_CC_END // This is }!!!! USING_NS_CC; // This is using namespace cocos2d; this can be a commonly used macro. Oh what? You can see that the programmer uses 'ns _ CC_END '-- 9 strings to replace the original' {'-- a character! God, is this a programmer who is still lazy in the bones? Is it true that laziness can put the horse upside down?