Reputation: 5354
How much integration do C++ and Objective C have in Objective C++? I realize that Objective C++ compiles both C++ and Objective C code, but do the languages actually interact with each other?
For example can I create a template of a Objective C object? How about inheriting an Objective C class from a C++ class?
What I am basically trying to ask is do the languages mix or do they simply compile together in the same file? To what extent?
Upvotes: 5
Views: 354
Reputation: 3542
Well it's quite common to use Objective-C++ to build applications. You can wrap Objective-C around C++ code but you can't share objects. For example you can't create an NSString object in Objective-C and trying to call the method hasSuffix: from C++ and visa versa. So to share content between those two you need to use C types like structs, chars, ints etc. It's because the Objective-C objects are only usable in the Objective-C runtime environment and the C++ objects are only usable in the C++ runtime environment. So the only thing they both share and can communicate through is C.
Maybe a little example will show you how you can interact between these two.
NSString * objcStr = [[NSString alloc] initWithString:@"Hello World!"];
std::string cppStr ([objcStr UTF8String]);
std::cout << cppStr;
Upvotes: 2
Reputation: 8937
You can call C++ Code from inside Objective-C and vise versa with some caveats. See How well is Objective-C++ supported? for more info
Upvotes: 1
Reputation: 107764
Have a look at the Objective-C language guide section on Objective-C++ and/or the Wikipedia article.
To answer your specific questions, yes the languages can "interact". Yes, you can create a template of an Objective-C object (id
is just a pointer) but don't expect to use RAII with Objective-C objects. No, you cannot derive an Objective-C class from a C++ class or a C++ class from an Objective-C class.
Upvotes: 2