Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6840951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:54:24+00:00 2026-05-26T23:54:24+00:00

Currently I am working on a cocos2d+Box2D project so I have deal with some

  • 0

Currently I am working on a cocos2d+Box2D project so I have deal with some Objective-C++ code.

And I am facing to such situation:

#import "cocos2d.h"
#import "Box2D.h"

@interface BasicNode : CCNode {
@private
    ccColor3B _color;
    b2Body *_body;
    b2Fixture *_shape;
}

b2Body and b2Fixture are C++ class that defined in Box2D.h

It works if the implementation of BasicNode is named BasicNode.mm.

But if I have another file named Game.m that is using BasicNode and import BasicNode.h, it won’t compile because .m file is Obj-C file and does not know about C++ code.

So I decided to move #import “Box2D.h” into implementation file and only keep type declaration in head file (this is exactly what header file should contain).

But how do I do it? They are C++ class type but they are actually just a pointer so I wrote some helper macro

#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 works, only if CLS_DEF(b2Body) is appear once only. Otherwise compiler will find multiple type declaration for a same name even they are the same. Than I have to change to

#ifdef __cplusplus
#define CLS_DEF(clsname) class clsname
#else
#define CLS_DEF(clsname) @class clsname
#endif

And it is working now.

But I don’t think it is a great idea that I declare a C++ class type as an Obj-C class especially I am using ARC.

Is any better way do deal with it? And I don’t really want to make 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 will my tweak way introduce any problem?? by making C++ class ivar looks like Obj-C class for other pure Obj-C code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:54:25+00:00Added an answer on May 26, 2026 at 11:54 pm

    There are a couple of ways. If you can rely on using the Objective-C 2.2 runtime’s features, you can add ivars in class (category) extensions. This means you can add ivars in your class’s .mm file, and keep the .h file clean of any C++ stuff.

    If you need to support older versions of the runtime, there are a few ways to do it which are better than #ifdefing. In my opinion, the best way is to use the ‘pimpl’ idiom which is common in C++ – you forward declare an implementation struct in your header, and add an ivar which is a pointer to such a struct. In your class’s implementation (.mm), you actually define that struct with all its C++ members. You then just need to allocate that implementation object in your init... method(s) with new and delete it in dealloc.

    I’ve written up the pimpl idiom as it applies to cleanly mixing Objective-C and C++ in this article – it also shows some other potential solutions which you could consider.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I`m currently working on a script, and I have the following situation. function somnicefunction()
Im currently working on a grizzly, spring and jersey project and i have encountered:
Iam currently working on a project where i have to read serial port continuously.
I am new to Objective-C. I am currently working on a game using Cocos2D
Hi all im working on an ipad app using cocos2d objective c and box2d.
Currently working on a project in MVC-3. Trying to put the following code in
Iam currently working on a iPad project where i build my code on top
I'm working on integrating Ben Gottlieb's Twitter-OAuth-iPhone code into my cocos2d 0.99.5 project using
Currently working on a VBScript to automate some of the dirty PST ingestion work
Currently working on a flex AIR project based on PureMVC framework. There was a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.