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.
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.
- Class Base {/* ... */};
- @interface objcclass:base ... @end//error!
- 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:
- @interface Foo {
- Class Bar {...}//OK
- }
- @end
- Bar *barptr; Ok
|
OC allows the C struct as an instance variable, regardless of whether it is declared inside the OC declaration.
- @interface Foo {
- struct Cstruct {. .. };
- struct cstruct bigivar;//OK
- } ... @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:
However, because it is a keyword, you cannot use class to do variable names:
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:
- id<someprotocolname> foo;
- 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:
- Label::: Global_name = 3;
|
A space is required after the first colon. Oc++ a similar situation, a space is also required:
- 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 + +