Integrated attention points and benefits of OC and C language

Source: Internet
Author: User

Apple's objective-c compiler approves users to freely mix and use C + + and objective-cin a unified source file, and the mixed language is called objective-c++. With it, you will be able to leverage the existing C + + class libraries in the objective-c utilization process.

Key to Objective-c and C + +

In objective-c++, the ability to invoke actions with C + + code can also be invoked from Objective-c. In both languages, objects are pointers and can be used anywhere. For example, a C + + class can take a pointer to a Objective-c object as a data member, and the Objective-c class can have C + + object pointers as instance variables. This is explained in the following example.

Beware: Xcode must have the source file ". mm" as the extension name, so the ability to enable compiler objective-c++ stretching.

  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;
  53. }

Just as you can declare C constructs in the OC interface, you can also declare C + + classes in the OC interface. Like the C construct, the C + + class defined in the OC interface is the overall scope, not the embedded class of the OC class (this is consistent with the specification C (although not C + +) progression to nested constructs defined as the file scope).

To allow you to conditionally write code based on a language variant, the Oc++ compiler defines the __cplusplus and __objc__ preprocessor constants, leaving the specified C + + and OC. As mentioned earlier, oc++ does not approve C + + classes to inherit from OC objects, nor does it approve OC classes to inherit from C + + objects.

    1. Class Base {};
    2. @inte ***ce objcclass:base ... @end//error!
    3. Class Derived:public Objcclass ...//error!

Unlike OC, C + + objects are static, and run-time polymorphism is a specific case. The object model of both languages is therefore not directly acceptable. More or less, OC and C + + objects are incompatible in the in-memory pattern, and even if it is not possible to create an object instance from both languages it works. Thus, two kinds of hierarchical constructions cannot be mixed.

You can declare C + + classes inside the OC class, and the compiler treats these classes as if they were declared in the big picture space. Just like the following:

    1. @inte ***ce Foo {
    2. Class Bar {...}//OK
    3. }
    4. @end
    5. Bar *barptr; Ok

OC approves the C construct as an instance variable, regardless of whether it is declared inside the OC declaration.

    1. @inte ***ce Foo {
    2. struct Cstruct {...};
    3. struct Cstruct Bigivar; Ok
    4. } ... @end

Mac OS X 10.4 Then, in case you set the Fobjc-call-cxx-cdtors compiler symbol, you will be able to customize the 0-parameter constructor, the destructor of C + + with the virtual function and the meaning of the user. Class instance as an instance variable (gcc-4.2 default compiler symbol Fobjc-call-cpp-cdtors). After the OC member Variable alloc, the Alloc function invokes the constructor in the order in which it is declared. The constructor takes advantage of a public parameterless, secure constructor. Before the OC member variable dealloc, the Dealloc measure calls the destructor in reverse order of the declaration sequence. OC did not know the concept of the nominal space. The OC class cannot be declared inside C + + nominal space, nor can it be declared in the OC class.

OC class, protocol, classification can not be declared in C + + template, C + + template can not be declared in the OC interface, the scope of the Protocol, classification.

However, the OC class can do the C + + template parameters, C + + template parameters can also do OC message confession type of air compressor receiver or parameters (cannot traverse selector).

C + + lexical ambiguity and conflict

Some identifiers are defined in the OC header file, and all OC processes are included, and these identifiers are Id,class,sel,imp and bool.

In OC measures, the compiler pre-declares identifiers immolation and super, just like the keyword this in C + +. Unlike C + + 's This, immolation and super are context-sensitive, and they can be used in addition to OC measures for easily identifiable identifiers.

parameter list of measures within the Protocol, with 5 context-coherent keywords (oneway,in,out,inout,bycopy). These are not keywords in other content.

From the OC's point of view, C + + added a lot of new keywords. You can still use C + + keywords to do a part of OC selector, so the contagion is not serious, but you can't use them to name OC classes and instance variables. For example, although class is a C + + keyword, you can still take advantage of NSObject's action class:

    1. [Foo class]; Ok

However, because it is a keyword, you cannot use class to do variable names:

    1. NSObject *class; Error

The class name and category name in OC have a namespace of their own. @inte ***ce foo and @inte***ce (foo) can exist in one source code at the same time. In oc++, you can also name your classification with the class name or construct name in C + +.

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

    1. id<someprotocolname> foo; Templatetype<sometypename> Bar;

To avoid this generalization, the compiler does not approve the ID as a template. Finally, C + + has a grammatical ambiguity, and when a label is followed by a confession to represent a big picture, it looks like this:

    1. Label::: Global_name = 3;

The first colon must be preceded by a space. Oc++ have a similar situation and must have a space:

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

Limitations

Oc++ has not acquired the functions of C + + for OC class, nor has it acquired OC function for C + + class. For example, you cannot invoke a C + + object with OC syntax, you cannot add constructors and destructors to OC objects, and you cannot rotate this and immolation with each other. The system structure of a class is self-reliant. C + + classes cannot inherit from the OC class, nor can the OC class inherit the C + + class. In addition, the treatment of multi-lingual disorders is not supported. That is, an OC thrown by the abnormal can not be captured by C + + code, anti-C + + code thrown out of the abnormal can not be captured by OC code. Especially those who are aware of the errors when reading the code, these errors often do not pass through the machine on the test to distinguish.

Integrated attention points and benefits of OC and C language

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.