Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of C++ in Objective C++?

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?

like image 312
fdh Avatar asked Jan 31 '12 03:01

fdh


People also ask

Can you use C in Objective-C?

“Using C in Objective-C” is somewhat misleading, because Objective-C is a strict superset of the C language. You really can't use C in Objective-C, since Objective-C is C.

What is the use of C?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Which is better C or Objective-C?

Objective C can be called the super set of C language. It contains classes and objects in addition to C language. The pointers used in C language are vulnerable to security attacks. The language objective C uses null pointers and hence is type safe compared to C.

What is Objective-C mainly used for?

Objective-C is general-purpose language that is developed on top of C Programming language by adding features of Small Talk programming language making it an object-oriented language. It is primarily used in developing iOS and Mac OS X operating systems as well as its applications.


1 Answers

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;
like image 185
dj bazzie wazzie Avatar answered Oct 11 '22 18:10

dj bazzie wazzie