I am currently working on a cocos2d + Box2D project, so I am dealing with some Objective-C ++ code.
And I came across a situation like this:
#import "cocos2d.h" #import "Box2D.h" @interface BasicNode : CCNode { @private ccColor3B _color; b2Body *_body; b2Fixture *_shape; }
b2Body and b2Fixture are the C ++ class defined in Box2D.h
It works if the implementation of BasicNode is called BasicNode.mm.
But if I have another file called Game.m that uses BasicNode and imports BasicNode.h, it will not compile because the .m file is an Obj-C file and does not know about C ++ code.
So, I decided to move #import "Box2D.h" to the implementation file and only save the type declaration in the head file (this is exactly what the header file should contain).
But how do I do this? This is a C ++ class type, but they are actually just a pointer, so I wrote some helper macros
#ifdef __cplusplus #define CLS_DEF(clsname) class clsname #else #define CLS_DEF(clsname) struct clsname; typedef struct clsname clsname #endif CLS_DEF(b2Body); CLS_DEF(b2Fixture);
It only works if CLS_DEF (b2Body) appears only once. Otherwise, the compiler will find several type declarations for the same name, even if they are the same. What should I switch to
#ifdef __cplusplus #define CLS_DEF(clsname) class clsname #else #define CLS_DEF(clsname) @class clsname #endif
And now it works.
But I don’t think it’s a great idea that I declare a C ++ class type as an Obj-C class, especially using ARC.
Is there a better way to handle this? And I really don't want to do something like this
@interface BasicNode : CCNode { @private ccColor3B _color; #ifdef __cplusplus b2Body *_body; b2Fixture *_shape; #else void *_body; void *_shape; #endif }
Edit: Also, please tell me if my setup method will be any problem? by creating a C ++ class, the ivar class looks like the Obj-C class for other pure Obj-C code.