Objective-C and C hybrid editing for iPhone Development

Source: Internet
Author: User

DetailsIPhone DevelopmentOfObjective-CAndCHybrid editing is the content to be introduced in this article. It mainly introducesIphone DevelopmentInObjective-CAndCFor more information, see the details.

Apple'sObjective-CThe compiler allows users to freely mix C ++ andObjective-CThe mixed language is called Objective-C ++. With it, you canObjective-CUse the existing C ++ class library in the process.

Key to Objective-C and C ++ hybrid editing

In Objective-C ++, you can use C ++ code to call a measure or use Objective-C to call a measure. In both languages, objects are pointers that can be used anywhere. For example, the C ++ class can use the pointer of the Objective-C object as a data member, and the Objective-C class can also have the C ++ object pointer as an instance variable. This is explained in the following example.

Caution: Xcode requires that the source file be named ". mm", so that the Objective-C ++ extension of the compiler can be enabled.

 
 
  1. #import <Foundation/Foundation.h> 
  2. class Hello {  
  3. private:  
  4. id greeting_text; // holds an NSString  
  5. public:  
  6. Hello() {  
  7. greeting_text = @"Hello, world!";  
  8. }  
  9. Hello(const char* initial_greeting_text) {  
  10. greeting_text = [[NSString alloc] initWithUTF8String:initial_greeting_text];  
  11. }  
  12. void say_hello() {  
  13. printf("%s/n", [greeting_text UTF8String]);  
  14. }  
  15. };  
  16. @inte***ce Greeting : NSObject {  
  17. @private  
  18. Hello *hello;  
  19. }  
  20. - (id)init;  
  21. - (void)dealloc;  
  22. - (void)sayGreeting;  
  23. - (void)sayGreeting:(Hello*)greeting;  
  24. @end  
  25. @implementation Greeting  
  26. - (id)init {  
  27. if (immolation = [super init]) {  
  28. hello = new Hello();  
  29. }  
  30. return immolation;  
  31. }  
  32. - (void)dealloc {  
  33. delete hello;  
  34. [super dealloc];  
  35. }  
  36. - (void)sayGreeting {  
  37. hello->say_hello();  
  38. }  
  39. - (void)sayGreeting:(Hello*)greeting {  
  40. greeting->say_hello();  
  41. }  
  42. @end  
  43. int main() {  
  44. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  45. Greeting *greeting = [[Greeting alloc] init];  
  46. [greeting sayGreeting]; // > Hello, world!  
  47. Hello *hello = new Hello(xj.xjwmz.com"Bonjour, monde!");  
  48. [greeting sayGreeting:hello]; // > Bonjour, monde!  
  49. delete hello;  
  50. [greeting release];  
  51. [pool release];  
  52. return 0;  

Just as you can declare the C structure in the OC interface, you can also declare the C ++ class in the OC interface. Like the C structure, the C ++ class defined in the OC interface is within the overall scope, not the embedded class of the OC class. This is similar to the standard C Although not C ++) the advance nesting structure is defined as a uniform file range ).

In order to approve the Conditional compilation of code based on language variants, the OC ++ compiler defines _ cplusplus and _ OBJC _ Preprocessor constants, respectively specifying C ++ and OC. As mentioned above, the OC ++ class does not approve that the C ++ class inherits from the OC object or that the OC class inherits from the C ++ object.

 
 
  1. class Base { };  
  2. @inte***ce ObjCClass: Base ... @end // ERROR!  
  3. class Derived: public ObjCClass ... // ERROR! 

Different from OC, C ++ objects are static types, and the polymorphism in running mode is special. The object models in the two languages are therefore unacceptable. Even more, the pattern of OC and C ++ objects in the memory is incompatible, it cannot be used to create an object instance in two languages. Therefore, the two types of hierarchical structures cannot be mixed.

You can declare C ++ classes within the OC class, And the compiler treats these classes as declared in the overall project space. Like the following:

 
 
  1. @inte***ce Foo {  
  2. class Bar { ... } // OK  
  3. }  
  4. @end  
  5. Bar *barPtr; // OK 

OC approves the C structure as an instance variable, whether or not it is declared within the OC declaration.

 
 
  1. @inte***ce Foo {  
  2. struct CStruct { ... };  
  3. struct CStruct bigIvar; // OK  
  4. } ... @end 

After Mac OS X 10.4, if you set the fobjc-call-cxx-cdtors compiler symbol, you can use the User-Defined zero-parameter constructor including virtual functions and so on, the destructor of the C ++ class instance as an instance variable gcc-4.2 default settings compiler symbol fobjc-call -cpp-cdtors ). After the OC member variable alloc is complete, the alloc function calls the constructor in the declared order. The constructor uses the common non-parameter-safe constructor. Before the OC member variable dealloc, The dealloc action calls the destructor in reverse order of declaration. OC does not have the concept of a well-known object space. You cannot declare the OC class in the C ++ project or the OC class.

The OC class, protocol, and classification cannot be declared in the C ++ template, and the C ++ template cannot be declared in the OC interface, protocol, or classification scope.

However, the OC class can be used as the C ++ template parameter, and the c ++ template parameter can also be used as the OC message expression-type air compressor receiver or parameter cannot cross selector ).

C ++ vocabulary ambiguity and conflict

The OC header file defines some identifiers, which are included in all OC processes. These identifiers recognize IDs, classes, SEL, IMP, and BOOL.

In the OC measure, the compiler predeclares the identifiers immolation and super, and considers the keyword this in C ++. Unlike this in C ++, immolation and super are contextual; they can also be used for idle identifiers in addition to OC measures.

The parameter list of the measures in the protocol, which has five contextual keywords oneway, in, out, inout, bycopy ). These are not keywords in other content.

From the perspective of OC process personnel, C ++ adds a lot of new keywords. You can still use the C ++ keyword as a part of the OC selector, so the impact is not serious, but you cannot use them to name the OC class and instance variables. For example, although class is the keyword of C ++, you can still use NSObject's measure class:

 
 
  1. [foo class]; // OK 

However, because it is a keyword, you cannot use class as a variable name:

 
 
  1. NSObject *class; // Error 

The class name and category name in OC have their own namespaces. @ Inte *** ce foo and @ inte *** ce (foo) can coexist in the same source code. In OC ++, you can also use the class name or constructor name in C ++ to name your category.

The protocol and template identifier use the same syntax but have different goals:

 
 
  1. id<someProtocolName> foo;TemplateType<SomeTypeName> bar; 

To avoid such generalization, the compiler does not approve the id as the template Name. Finally, C ++ has a syntax ambiguity. When a label is followed by a confession to indicate a big picture name, it is like the following:

 
 
  1. label: ::global_name = 3; 

The first colon must be followed by a space. There is a similar situation in OC ++, and a space is also required:

 
 
  1. receiver selector: ::global_c++_name; 

Limitations

OC ++ has not added the C ++ function for the OC class, nor has it added the OC Function for the C ++ class. For example, you cannot use the OC syntax to call C ++ objects, nor add constructor and destructor to OC objects. You cannot rotate this and immolation. Class system structure is self-reliant. C ++ classes cannot inherit OC classes, and OC classes cannot inherit C ++ classes. In addition, multi-language troubleshooting is not supported. That is to say, exceptions thrown by an OC cannot be caught by C ++ code. Exceptions thrown by C ++ Code cannot be caught by OC code. Especially those mistakes that can be noticed rashly when reading Code, these mistakes are often not identified by quizzes on the machine.

Summary: DetailsIPhone DevelopmentOfObjective-CAndCI hope this article will help you with the introduction of mixed compilation!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.