C ++: What should I pay attention to when turning to Objective-C (basic)

Source: Internet
Author: User

From C ++ to Objective-C
Objective-C does not contain multiple inheritance, namespaces, Operator overloading, templates, class variables, abstract classes, STL, and other structures. However, Objective-C provides a similar structure.

Dynamic Allocation of C ++ virtual tables and Objective-C
The biggest difference between C ++ and Objective-C is that the allocation method (C ++ becomes a member function) has different mechanisms. C ++ uses the virtual table mechanism to determine the code called by the virtual function.

It can be considered that each C ++ object has a pointer pointing to the function pointer array.
After the compiler knows that the code needs to call a virtual function, it calculates the cheap location from the beginning of the virtual table, and sends the machine code to point the function pointer to the offset location, then execute the code snippet program at this location. This process requires the compiler to know the object type of the member function to be called during compilation (that is why some types need to be forcibly converted when writing C ++ code ), in this way, the offset position of the virtual table can be correctly calculated. This allocation is very fast. Only a few pointer operations and a read operation for getting function pointers are required.

 

 
 

Objective-C uses the runtime function to enter various types of structures and find the corresponding code for calling.
This technology is much slower than the routing of C ++. Objective-C sacrifices a certain amount of speed and security for flexibility and convenience. In C ++, C ++ is very secure because the compiler can ensure that the object can process the corresponding methods. However, it lacks flexibility.


Here we will talk about the isa pointer. NSObject has a member variable of Class isa pointer type, because most of our objects directly or indirectly inherit from NSObject, so they will all inherit this isa member variable, isa points to the Class Object of the object at runtime. The Class object of all objects in a Class is the same (same for JAVA ), this ensures that each type in the memory has a unique type description. This Class object also has an isa pointer, which points to the Class Object of the parent Class at the upper level.

After understanding this isa, you can understand that when inheriting, A inherits B, you call method A () of (), first, isa of A goes to the Class Object of A to find the () method. If it finds it, it is called. If it does not, the isa in the Class Object of A is driven to search for the Class Object of the parent Class B.

The C ++ compiler no longer retains many class information, such as the class inheritance chain and class members. Generally, the ability of C ++ to process objects at runtime is limited, and a maximum of dynamic forced type conversion can be performed. This operation can be used to check whether an object is a specified subclass of another object.

C ++ cannot change the hierarchy during runtime. After the program is compiled and linked, the program is almost fixed. In Objective-C, an object only needs methods to implement its own callability. In this way, any object can be the data source and delegate of other objects. Lack of Multi-inheritance may cause us a lot of inconvenience, but we do not have to consider its inheritance lineage when sending any message to any object, this greatly simplifies our work.

Obviously, compared with C ++, it is much less secure to send any message to any object and C ++ is very secure because the compiler can ensure that the object can process the corresponding methods. Type security. If the object receiving the message cannot process the message, you will get a running error. There is no type security container in Cocoa, and any object can be put into a container.

Objective-C carries a lot of metadata about classes. Therefore, you can use reflection to determine whether an object corresponds to a message. This method is often used for objects that contain data sources or delegates. However, you must first check whether the delegate has a message to avoid possible runtime errors. Therefore, you can add methods to other classes by category.

With metadata, you can easily operate the classes used in the program. You can determine the instance variables, the layout of the instance variables in the object, and the class definition method. Even deleting the debugging information of an executable object does not delete the metadata of Objective-C.

In Objective-C, you can directly send messages to nil objects without checking whether the sent messages are NULL.


All Objective-C objects are dynamically allocated addresses.

There are no stack-based objects, no automatic creation and destruction of temporary objects, no automatic creation and destruction of temporary objects, and no automatic type conversion between types. Therefore, Objective-C objects are more complex than stack-based C ++ objects. In this way, it is safe to use the object address as the Environment pointer, so you don't have to worry that some stack-allocated objects will disappear. (For example, if the pointer of a local automatic variable is returned in C ++, the program crashes when the pointer is used again ).

Metadata
Data in any file system is divided into data and metadata. Data refers to the actual data in a common file, and Metadata refers to the system data used to describe the features of a file, such as access permissions, file owners, and distribution of file data blocks (inode ...) and so on. In the cluster file system, the distribution information includes the location of the file on the disk and the location of the disk in the cluster. You must obtain the metadata of a file before you can locate the file and obtain the file content or related attributes.

This seems to be the metadata in the database. The metadata of Objective-C has not yet been explained.

 

 

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.