2

I need to know what is the functionality of this sentence in Cocos2D-X in C++:

USING_NS_CC

Example in code:

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "GameOverScene.h"

USING_NS_CC;

HelloWorld::~HelloWorld()
{
    if (_targets)
    {
        _targets->release();
        _targets = NULL;
    }

    if (_projectiles)
    {
        _projectiles->release();
        _projectiles = NULL;
    }

    // cpp don't need to call super dealloc
    // virtual destructor will do this
}
rene
  • 41,474
  • 78
  • 114
  • 152

1 Answers1

6

According to cocos2d-x source code macro is an equivalent of using namespace cocos2d.

You can check it on your own in CCPlatformMacros.h in cocos2d-x repository:

#ifdef __cplusplus
    #define NS_CC_BEGIN                     namespace cocos2d {
    #define NS_CC_END                       }
    #define USING_NS_CC                     using namespace cocos2d
    #define NS_CC                           ::cocos2d
#else
    #define NS_CC_BEGIN 
    #define NS_CC_END 
    #define USING_NS_CC 
    #define NS_CC
#endif 

In response to @rene answer, I checked "Learning Cocos2d-x Game Development" from author Siddharth Shekar and the author is wrong.

Here is a link to that paragraph from this book, here is an image of the paragraph.

What he wrote wrong about USING_NS_CC:

  • it doesn't "#include CCPlatformMacros.h" and it doesn't load additional macros

    To use it you need to include CCPlatformMacros.h directly by adding #include <cocos/CCPlatformMacros.h> or indirectly by adding other header that contains it, e.g. #include <cocos2d.h>.

  • it doesn't set the namespace to cocos2d

    It includes all symbols from cocos2d namespace, what makes that you can use cocos2d-x symbols without pointing the right cocos2d::.

    To set a namespace cocos2d for you symbols you should use

    namespace cocos2d
    {
      // some declarations/definitions
    }
    

    or equivalent:

    NS_CC_BEGIN
    
      // some declarations/definitions
    
    NS_CC_END
    

    But remember that adding your own symbols to external library is strongly not recommended.

Greg Rynkowski
  • 556
  • 6
  • 20
  • do you know what is the difference between "namespace cocos2" and "using namespace cocos2d" ? and what is the purpose of the #else statement if they declare the same macros as in the "ifdef ___cplusplus" ? – isJulian00 Oct 20 '18 at 00:45
  • `namespace cocos2 { /*your code*/ }` establish the namespace for the code inside of the brackets, so all the symbols (global variables, classes, functions) you add is going to be added to that namespace. As a game dev, when you are not the owner of the namespace, it is not a good idea to `use namespace cocos2d {}` as you may encounter name conflicts. `using namespace cocos2d` on the other hand, just eliminate the need to write the namespace name `cocos2d::` on the left-hand side of all symbols from cocos2d namespace, but it doesn't add any new symbol to cocos2d namespace. – Greg Rynkowski Aug 10 '19 at 11:00
  • Regarding the purpose of the `#else`: what would happen if `___cplusplus` was not defined, e.g. in a C or Objective-C code? We would have a compilation problem since some symbols like `NS_CC` doesn't exist. That's the reason. `#ifdef __cplusplus ... #else .. #endif` is a mechanism to ensure successful compilation of compilation units that don't support specific C++ features, like namespaces in this scenario. – Greg Rynkowski Aug 10 '19 at 11:05