in iOS

Last Objective-C Annoyance: Fixed!

I’ve gone on and on before about how much I like Objective-C and especially, how well it plays with C++. Mixing the two is a pleasure, and it’s often hard to remember where one stops and the other one starts. So much so that sometimes I catch myself calling C++ member functions like this [self myFunction:param];. Oops.

It is true that Objective-C can be a bit more verbose than plain C++, but it more than makes up for it with named parameters (which increase readability a huge amount), and not having to duplicate many things between header and implementation files.

Yet, even though I like Objective-C so much, there was one last, really annoying feature that was preventing perfect interop between C++ and Objective-C. Something so simple, yet so fundamental, that it really got in the way. Something that wouldn’t allow me to write something as simple as this:

@interface MyView : UIView
{
    // ...
    SingleTouchEvent m_singleTouchEvent;
}

SingleTouchEvent is a custom C++ class. There’s nothing wrong with that right? There shouldn’t. But if you try and compile it, you’ll get this warning:

warning: type `SingleTouchEvent' has a user-defined constructor
warning: C++ constructors and destructors will not be invoked for Objective-C fields

Some of you might be saying, no big deal, you can dynamically allocate objects and have their constructors called as usual. That’s true, but I hate allocating objects on the heap just because of something like this. My preference is always to make them member variables if I can. It’s faster, more efficient, less error prone, and easier to read. Needless to say, this was bugging the hell out of me.

I looked all over the build options in case there was some setting I could toggle to enable C++ constructors to be called, but no luck. I did a bit more research, and lo and behold, it turns out there is a custom setting you can pass directly to gcc to do exactly that! It’s nowhere in the XCode settings GUI, so you have to enter it by hand. Go to Project | Info, and in the Custom Settings section, enter the following: GCC_OBJC_CALL_CXX_CDTORS = YES

setting

With that setting enabled, Objective-C and C++ now play together better than ever!

Without any barriers to writing new code in Objective-C, I expect it will slowly (or maybe not so slowly) replace C++ as my main language of choice for future projects.

  • I’ve found that using Objective C types as template parameters doesn’t work for function overloading. I think the compiler sees all objective C objects as type objc_type*. I forget the exact situation… I think it was something like…

    template class RetainedPtr;

    void sribbleDee(RetainedPtr labelToScribble);
    void sribbleDee(RetainedPtr viewToScribble);

    I’ll see if I can dig it up.

  • Matthew Wiggins

    I recently had the same revelation. As of SDK 2.2.1, the option does appear in the project settings as ‘Call C++ Default Ctors/Dtors in Objective C’ – at least in the new projects I’ve created.

    What code do you expect to keep in C++ versus ObjC? So far I’ve only kept matrix & vector math in C++ to take advantage of operator overloading.

  • Alex

    I don’t suppose you know why that isn’t the default?
    It seems outrageous to me that it’s not…

  • Great Scott! That is one handy hidden build setting! Thanks for sharing.

  • Thanks! This is a tremendous help for a C++ programmer trying to develop for the iPhone. (I don’t like the Objective-C syntax, so I’m using mostly C++ classes.)