Objective-CLearning notes are the content to be introduced in this article. As mentioned above, C ++ won't let youObjective-C objectInherits C ++ classes and does not allow youObjectInheritanceObjective-CClass.
- class Base { };
- @interface ObjCClass: Base ... @end // ERROR!
- class Derived: public ObjCClass ... // ERROR!
Unlike Objective-C, objects in C ++ are static, and running polymorphism is considered an exception. Therefore, the object models of the two languages cannot be directly compatible. More fundamentally, the memory layout of Objective-C and C ++ objects is mutually contradictory.
This means that it is generally impossible to create an object instance effective in both languages. Therefore, these two types of hierarchies cannot be used together.
You can declare a C ++ class in a declared Objective-C class. The compiler has been considered to declare such a class in the global namespace, as follows:
- @interface Foo {
- class Bar { ... } // OK
- }
-
- @end
- Bar *barPtr; // OK
Objective-C allows the C structure, whether declared or not in Objective-C) as an instance variable.
- @interface Foo {
- struct CStruct { ... };
- struct CStruct bigIvar; // OK
-
- }
- ...
- @end
Objective-C is making similar efforts to make C ++ class instances as instance variables. As long as c ++ does not
The definition of a virtual member function is just a problem with all of its superclasses. If any virtual member function exists, the C ++ class cannot be used as an Objective-C instance variable.
- #import <Cocoa/Cocoa.h>
- struct Class0 { void foo(); };
- struct Class1 { virtual void foo(); };
- struct Class2 { Class2(int i, int j); };
- @interface Foo : NSObject {
- Class0 class0; // OK
- Class1 class1; // ERROR!
- Class1 *ptr; // OK—call 'ptr = new Class1()' from Foo's init,
- // 'delete ptr' from Foo's dealloc
- Class2 class2; // WARNING - constructor not called!
- }
- …
- @end
In C ++, each instance of classes containing virtual functions must contain a pointer to an appropriate virtual function table. However, virtual objects cannot be initialized during Objective-C runtime.
Function table pointer. Because it is not like the model of the C ++ object, it is also not required to be dispatched to the C ++ constructor or destructor during Objective-C runtime. For example, any user-defined structure or destructor of a C ++ class
They are not called. The compiler sends a warning in this case.
Objective-C does not have a nested namespace concept. You cannot declare the Objective-C Class in the C ++ namespace, or declare the namespace in the Objective-C class.
Objective-C classes, protocols, and categories cannot be declared in C ++TemplateAnd cannot declare a C ++ template within the range of an Objective-c interface, protocol, or category.
However, Objective-C classes can be used as C ++TemplateParameters. In the Objective-C information expression, the C ++ template can also be used as a receiver or parameter, but not as a selector parameter ).
Summary:Objective-CI hope this article will be helpful to you!