Questions tagged [objective-c++]

Objective-C++ is the union of the Objective-C and C++ syntax sets.

Objective-C++ is the union of the Objective-C and C++ syntax sets. Objective-C is, by definition, a strict superset of the C language; C++ is similarly regarded as such in practice (albeit without qualifying as a strict superset, q.v. this illustrated answer) and the conceptual union of these two supersets is the basis for the Objective-C++ language.

The practical use of Objective-C++ is contingent on the compiler in question. Briefly, compiling in Objective-C++ mode puts all of the basic C-family compiler settings in play, plus all C++ extensions, plus all Objective-C extensions, plus its own niche concerns (e.g. its ABI corner-case behavior). Despite this preponderance of abusable options, it is pretty straightforward to compile Objective-C++, as part of a larger project (as with parts of OpenFrameworks) or as the basis for a project in its own right (i.e. Pivotal Labs’ Cedar).

For the most part, two languages can be fairly freely intermixed – one important caveat being a class definition in C++ cannot inherit from an Objective-C class; likewise, Objective-C classes can’t inherit from C++ classes.

One can define Objective-C classes with:

  • pointer ivars for pointers to dynamically allocated instances of C++ types

  • value ivars for inline storage of C++ data, for default-constructable types

  • class and instance methods that receive and/or return C++ types

    • pass by value, by pointer, by reference (lvalue or rvalue‡)
  • std::atomic values accessible concurrently from both NSThread and GCD alongside std::thread primitives‡

One can define C++ classes:

  • containing pointer-to-Objective-C-class members (e.g. NSURL*, NSString*, NSImage* etc)
    • With caveats on object lifecycle management qua ARC and GC
  • as templates accepting NSObject subclasses as typename parameters
  • with method members that accept and/or return NSObject subclass instances
    • By pointer as per [OCType alloc] message return value
  • with method template members that accept NSObject subclasses as typename parameters
  • that use block pointers as expected (e.g. in typedef and using‡ declarations)

Notably, Objective-C++ does not allow for:

  • Templated Objective-C class structures of any kind – template <…> declaration syntax is incompatible with the @implementation/@interface definition-marker grammar rules
  • Templated Objective-C class or instance methods – e.g. the following code is tempting to try, but totally invalid:
@implementation NSSomeThing : NSObject

    template <typename T>
    + (T) getValue {
        return static_cast<T>(value);
    }

@end

… C++ templates and Objective-C classes use separate, incompatible preprocessing paths in their respective compilation processes.


‡ may be used with C++11 compiler support

1234 questions
195
votes
2 answers

What is Objective C++?

What is Objective C++ and can I use this language in Xcode?
Saurabh
  • 22,743
  • 12
  • 84
  • 133
135
votes
2 answers

How to use the same C++ code for Android and iOS?

Android with NDK has support to C/C++ code and iOS with Objective-C++ has support too, so how can I write applications with native C/C++ code shared between Android and iOS?
ademar111190
  • 14,215
  • 14
  • 85
  • 114
111
votes
6 answers

How to convert std::string to NSString?

I am trying to convert a standard std::string into an NSString but I'm not having much luck. I can convert successfully from an NSString to a std::string with the following code NSString *realm = @"Hollywood"; std::string REALM = [realm…
Anthony McCormick
  • 2,724
  • 3
  • 25
  • 27
109
votes
3 answers

How do I convert a NSString into a std::string?

I have an NSString object and want to convert it into a std::string. How do I do this in Objective-C++?
gabor
  • 4,281
  • 8
  • 24
  • 20
75
votes
3 answers

C Language: what does the .mm extension stand for?

A project with some Objective-C has a few C classes with the implementation files having a .mm extension. file.h file.mm What does the .mm mean? Shouldn't it just be .m?
aneuryzm
  • 63,052
  • 100
  • 273
  • 488
72
votes
2 answers

Implicit instantiation of undefined template 'std::basic_string, std::allocator >'

In the project I have been working on, we have to send Cocoa notifications from C++ sub-projects to the main project above it. To do this we construct a map to act as a key-value store for the userInfo dictionary of the notification. In one of the…
Andrew
  • 821
  • 1
  • 6
  • 7
44
votes
5 answers

Mixing Objective-C and C++

I'm trying to mix Objective-C with C++. When I compile the code, I get several errors. A.h #import #include "B.h" @interface A : NSView { B *b; } -(void) setB: (B *) theB; @end A.m #import "A.h" @implementation A -…
LandonSchropp
  • 10,084
  • 22
  • 86
  • 149
33
votes
7 answers

How well is Objective-C++ supported?

I've been learning Objective-C and Cocoa by working my way through the Hillegass book and it occurs to me that I might be better off using Objective-C++. Objective-C seems like the clear choice for developing UIs but I have a very strong C++…
Ferruccio
  • 98,941
  • 38
  • 226
  • 299
32
votes
2 answers

Objective-C, .m / .mm performance difference?

I tend to use the .mm extension by default when creating new classes so that I can use ObjC++ later on if I require it. Is there any disadvantage to doing this? When would you prefer .m? Does .m compile to a faster executable (since C is generally…
Sam
  • 3,659
  • 3
  • 36
  • 49
26
votes
2 answers

Linker error calling C-Function from Objective-C++

I've got a weird linker issue. I have code that looks like so: double given_amount = self.modelController.levelCompleteRewardAmount; swrve_currency_given(swrve, (CFStringRef)@"currencyName", given_amount); I have this code in two separate…
Chris Hill
  • 1,914
  • 1
  • 16
  • 24
25
votes
1 answer

Objective-C - Disadvantages to Bridging With C++?

So, I was bored today, and decide to mess with C++/Obj-C interpolation, and I found a way to create a very interesting setup. @protocol NSCPPObj -(id) init; -(id) initWithInt:(int) value; -(int) somethingThatReturnsAValue; -(void)…
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
24
votes
6 answers

Compiling external C++ library for use with iOS project

I'm completely new to using C++ libraries, so appreciate this might be a bit specific for my case (let me know and I can provide more details). I have an external C++ library that I'm trying to use with an iOS project. The library follows a…
HHHH
  • 1,197
  • 2
  • 16
  • 28
22
votes
2 answers

How do I change "Open" to "Select" in the NSOpenPanel?

In my Application i need to show the select file dialog, I am making use of the NSOpenPanel which allows to select the file, code is as shown below, - (IBAction)sendFileButtonAction:(id)sender{ NSOpenPanel* openDlg = [NSOpenPanel…
Amitg2k12
  • 3,765
  • 10
  • 48
  • 97
22
votes
2 answers

How can I use C++ with Objective-C in XCode

I want to use/reuse C++ object with Objective-C. I have a hello.h that has the class definition, and hello.cpp for class implementation. class Hello { int getX() ... }; And I use this class in Objective-C function. #include "hello.h" ... -…
prosseek
  • 182,215
  • 215
  • 566
  • 871
20
votes
13 answers

Duplicate interface declaration for class 'Foo'

I was working on my program, and it seems something in the settings changed. Suddenly I have the error "Duplicate interface declaration for class 'Foo'". It mentions a header file being duplicated but there's only one copy. Interestingly this is…
John Smith
  • 12,491
  • 18
  • 65
  • 111
1
2 3
82 83