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 6782499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:44:58+00:00 2026-05-26T16:44:58+00:00

I’m writing a large project for iOS in Objective-C++. I’m mainly using Objective-C for

  • 0

I’m writing a large project for iOS in Objective-C++. I’m mainly using Objective-C for the UI and other Apple APIs, and C++ for internal audio processing and other information handling. I was wondering about the drawbacks of mixing Objective-C and C++ freely.

Of course, mixing two object models has its inherent limitations and potential for messiness and confusion. I’m more curious about how using Objective-C++ will affect the compilation process, syntactic pitfalls I might run into, problems with readability and how I might avoid those, etc. I’m interested to hear what your experiences with Objective-C++ have been like and tips you might have for approaching this.

  • 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-26T16:44:58+00:00Added an answer on May 26, 2026 at 4:44 pm

    ObjC++ is extremely powerful – you can select and mix the features you need for your problems and interface with C, ObjC, and C++ all at the same time. I’ve been using it for many years. There are of course, a few caveats, and it’s good to be aware of them so you can minimize the issues you might encounter:

    Compilation

    The compilation times are much higher than ObjC or C++ when you begin creating nontrivial programs.

    There are a few common approaches to declaring your C++ types in ObjC types:

    • Opaque types
    • Forward Declarations
    • Forward Declarations with smart pointers
    • By value

    I’ll just gloss over this, as it is inferred from the OP that you are familiar with both languages. As well, this is one of the more publicly written about introductory topics on ObjC++.

    Given the C++ type:

    class t_thing { public: int a; };
    

    You have a number of ways to declare your ivars:

    Opaque type:

    @interface MONClass : NSObject { void* thing; } @end
    

    This should be avoided. It’s not good to erase type safety. The two forward options will introduce type safety.

    This variant is compatible with ObjC translations.

    Forward Declaration:

    class t_thing;
    @interface MONClass : NSObject { t_thing* thing; } @end
    

    This is better than an opaque type, but the smart pointer is even better – pretty obvious if you are used to writing modern C++.

    This variant is compatible with ObjC translations as long as your C++ types are in the global namespace.

    Forward Declaration using smart pointers:

    class t_thing;
    @interface MONClass : NSObject { t_smart_pointer<t_thing> thing; } @end
    

    This one is the best if you intend to set up translation firewalls (e.g. use PIMPL and forwards to reduce dependencies). As well, the ObjC object is already going through locking and allocations, so it’s not a bad point to allocate a C++ type. If you have several declarations, you may prefer to create a wrapper type for your implementation to reduce individual allocations.

    This variant is not compatible with ObjC translations.

    This is a good time to remind you that there is a compiler option with ObjC++ that you should enable: GCC_OBJC_CALL_CXX_CDTORS. What happens when this flag is set? The compiler produces hidden objc methods which invoke your C++ ivars’ constructors and destructors. If you use GCC_OBJC_CALL_CXX_CDTORS your C++ ivars must be default constructible. If you do not enable this flag, you must manually construct and destruct your ivars perfectly – if you construct it twice or do not override an initializer of the subclass, then you are facing UB.

    By value:

    #include "thing.hpp"    
    @interface MONClass : NSObject { t_thing thing; } @end
    

    Highest dependency. This is (in general) the route I chose, and I have some regrets about that. I’ve just moved things over to use more C++ and use composition with smart pointers (outlined above) to reduce dependency.

    This variant is not compatible with ObjC translations.

    One other thing about the modern ObjC compilers: The compiler lays out your C++ types’ ivars/structure in the binary. Believe it or not, this can consume a lot of binary space.

    The point here is that there are multiple forms the program can take. You can mix these techniques to reduce dependency, and this is one of the best places to introduce dependency firewalls because ObjC is very dynamic (its methods must be exported in one translation), and object creation requires allocations, locks, introduction into the reference counting system – initialization time for a single object is already relatively high, and the implementation will always be hidden.

    If much of your program is still in ObjC and you want to keep it that way, then you will need to resort to forwards of types which are declared in the global namespace or opaque base types which you vend specializations through an object factory. Personally, I just use so much C++ that this was not an ideal option, and wrapping implementations in global types quickly became tiresome.

    Meanwhile, since compilation times are high, the inverse is true: If you can keep significant portions of your implementation as C++, then you will save a lot of compilation time. For this reason and ARC (below) you can gain a lot by keeping your primitive Apple types as CF types where possible, so you can continue building C++ programs without the ObjC extensions.

    Syntax

    I rarely have problems but I keep my C++ classes quite strict:

    • I prohibit copy and assignment by default.
    • I rarely declare customized operators for C++ types.

    If you’re awesome at C++, then you could avoid this problem, but I prefer the compiler to catch silly mistakes I make.

    One evident problem is C++ scope resolution within an ObjC message send. This requires a space:

    [obj setValue:::func(a)]; // << bad
    [obj setValue: ::func(a)]; // << good
    

    Readability

    One problem I have encountered is that I have never found a code formatter that supports the ObjC++ syntax well.

    ObjC Messaging

    • ObjC Messaging and return by value: You need to check before messaging nil when returning C++ types by value. If the object you message is nil, then the result will be zeroed memory on the modern runtimes (x86_64 and iOS). If you use that instance, it is undefined behaviour.

    • ObjC Messaging and return by reference: You need to check before messaging nil when returning C++ types by reference. If the object you message is nil, then the result will be undefined behaviour (reference to 0/NULL).

    To overcome the ObjC Messaging issues, I typically use a form like this:

    - (bool)selector:(std::string&)outValue;
    

    where the return value is false for some internal error, and true for success.

    then you can safely write:

    if (![obj selector:outString]) { /* bail here */ }
    

    Miscellanea

    • ARC Compatibility: ObjC++ is not good for ARC. The primary reason is that ARC does not follow through mixed object models. Example: If you try to put a ObjC member into a C++ type, the compiler will reject the program under ARC. This is not really an issue because MRC is dead simple with ObjC++ (assuming you also use SBRM), but it may be a concern for the lifetime of your program.

    • Synthesized Properties: You will have to define your properties for C++ types.

    • External Tools: Beyond Xcode’s toolset, there are few programs that handle or recognize ObjC++ well. Text editors, IDEs, utilities.

    • Apple’s Tools: Within Xcode’s utilities, Xcode’s support for ObjC++ is a bit low. Refactoring (unavailable), navigation (improved with clang parser), outlining (is rather primitive), ObjC++ can disrupt IB’s utilities, project upgrading is often not supported.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am writing an app with both english and french support. The app requests
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload

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.