Objective-C learning notes

Source: Internet
Author: User

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.

 
 
  1. class Base { };  
  2. @interface ObjCClass: Base ... @end // ERROR!  
  3. 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:

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

Objective-C allows the C structure, whether declared or not in Objective-C) as an instance variable.

 
 
  1. @interface Foo {  
  2.     struct CStruct { ... };  
  3.     struct CStruct bigIvar; // OK  
  4.  
  5. }  
  6. ...  
  7. @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.

 
 
  1. #import <Cocoa/Cocoa.h> 
  2. struct Class0 { void foo(); };  
  3. struct Class1 { virtual void foo(); };  
  4. struct Class2 { Class2(int i, int j); };  
  5. @interface Foo : NSObject {  
  6.    Class0 class0;      // OK  
  7.     Class1 class1;      // ERROR!  
  8.     Class1 *ptr;        // OK—call 'ptr = new Class1()' from Foo's init,  
  9.     // 'delete ptr' from Foo's dealloc  
  10.     Class2 class2;      // WARNING - constructor not called!  
  11. }  
  12. …  
  13. @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!

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.