Key points of Objective-c and C + +

Source: Internet
Author: User
Tags object model

Using C + + with Objective-c
Apple's Objective-c compiler allows users to freely mix C + + and objective-c in the same source file, with a mixed language called objective-c++. With it, you can use an existing C + + class library in your OBJECTIVE-C application.

Key points of Objective-c and C + +
In objective-c++, you can call a method with C + + code or you can call a method from Objective-c. In both languages, objects are pointers and can be used everywhere. For example, a C + + class can use 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 illustrated in the following example.
Note: Xcode requires the source file to have a ". mm" extension in order to start the compiler's objective-c++ extension.


  1. /* HELLO.MM
  2. * Compile with:g++-x objective-c++-framework Foundation hello.mm-o Hello
  3. */
  4. #import <Foundation/Foundation.h>
  5. Class Hello {
  6. Private
  7. ID Greeting_text; Holds an NSString
  8. Public
  9. Hello () {
  10. Greeting_text = @ "Hello, world!";
  11. }
  12. Hello (const char* initial_greeting_text) {
  13. Greeting_text = [[NSString alloc] initwithutf8string:initial_greeting_text];
  14. }
  15. void Say_hello () {
  16. printf ("%s/n", [Greeting_text utf8string]);
  17. }
  18. };
  19. @interface Greeting:nsobject {
  20. @private
  21. Hello *hello;
  22. }
  23. -(ID) init;
  24. -(void) dealloc;
  25. -(void) saygreeting;
  26. -(void) saygreeting: (hello*) greeting;
  27. @end
  28. @implementation Greeting
  29. -(ID) init {
  30. if (self = [super init]) {
  31. Hello = new Hello ();
  32. }
  33. return self;
  34. }
  35. -(void) Dealloc {
  36. Delete Hello;
  37. [Super Dealloc];
  38. }
  39. -(void) saygreeting {
  40. Hello->say_hello ();
  41. }
  42. -(void) saygreeting: (hello*) Greeting {
  43. Greeting->say_hello ();
  44. }
  45. @end
  46. int main () {
  47. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  48. Greeting *greeting = [[Greeting alloc] init];
  49. [Greeting saygreeting]; > Hello, world!
  50. Hello *hello = new Hello ("Bonjour, monde!");
  51. [Greeting Saygreeting:hello]; > Bonjour, monde!
  52. Delete Hello;
  53. [Greeting release];
  54. [Pool release];
  55. return 0;
  56. }


As you can declare C structures in the OC interface, you can also declare C + + classes in the OC interface. Like the C structure, the C + + class defined in the OC interface is global in scope, not an inline class for the OC class (this is consistent with the standard C (though not C + +) elevated nested structure definition for file scope).
To allow you to conditionally write code based on a language variant, the Oc++ compiler defines the __cplusplus and __objc__ preprocessor constants, specifying C + + and OC, respectively. As mentioned earlier, oc++ does not allow C + + classes to inherit from OC objects, nor does it allow OC classes to inherit from C + + objects.


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


Unlike OC, C + + objects are statically typed and run-time polymorphic is a special case. The object model of both languages is therefore not directly compatible. More fundamentally, the layout of OC and C + + objects in memory is incompatible, that is, it is generally not possible to create an object instance that is valid from both languages ' point of view. Therefore, two types of hierarchies cannot be blended.
You can declare C + + classes inside the OC class, and the compiler treats these classes as if they were declared in the global namespace. Just like the following:


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


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


  1. @interface 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 flag, you can use a C + + class instance that contains a virtual function and a meaningful user-defined 0-parameter constructor, destructor, as an instance variable (gcc-4.2 default compiler flag 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 uses a public parameterless appropriate constructor. Before the OC member variable dealloc, the Dealloc method calls the destructor in order of the declaration sequence.     oc does not have a namespace concept. The OC class cannot be declared inside the C + + namespace, 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 parameters of C + + template, and the C + + template parameter can also be the receiver or parameter of the OC message expression (cannot pass selector). The

C + + lexical ambiguity and conflict
contains identifiers defined in the OC header file, which all OC programs must contain, which recognize Id,class,sel,imp and bool. Within the
OC method, the compiler pre-declares the identifier self and super, just like the keyword this in C + +. Unlike this C + +, self and super are context-sensitive; they can also be used for ordinary identifiers outside of the OC method.
A parameter list of methods within the protocol, with 5 context-sensitive keywords (oneway,in,out,inout,bycopy). These are not keywords in other content.
from the OC Programmer's Point of view, C + + adds a lot of new keywords. You can still use C + + keywords as part of OC selector, so the impact 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 use NSObject's method class:


  1. [Foo class]; Ok


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


  1. NSObject *class; Error


There is a separate namespace for class names and classification names in OC. @interface foo and @interface (foo) can exist in one source code at the same time. In oc++, you can also name your classification with the class name or struct name in C + +.
The protocol and template identifiers use the same syntax but with different purposes:


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


To avoid this ambiguity, the compiler does not allow the ID to be the template name. Finally, C + + has a syntactic ambiguity, and when a label is followed by an expression that represents a global name, it looks like this:


  1. Label::: Global_name = 3;


A space is required after the first colon. Oc++ a similar situation, a space is also required:


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



Limit
Oc++ does not increase the functionality of C + + for the OC class, nor does it add OC functionality for C + + classes. For example, you cannot call C + + objects with OC syntax, you cannot add constructors and destructors to OC objects, and you cannot replace this and self with each other. The architecture of the class is independent. C + + classes cannot inherit from the OC class, nor can the OC class inherit the C + + class. In addition, multi-language exception handling is not supported. In other words, an OC-thrown exception cannot be captured by C + + code, which in turn cannot be caught by the OC code.

Key points of Objective-c and C + +

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.