tailieunhanh - Learn Objective C on the Mac phần 10

Bạn có thể suy nghĩ của từng đối tượng C + + là có một con trỏ đến một mảng của con trỏ chức năng. Khi trình biên dịch thấy rằng mã muốn gọi một chức năng ảo, nó tính toán bù đắp từ sự khởi đầu của vtable, phát ra mã máy để có những con trỏ chức năng bù đắp từ khi bắt đầu của vtable, và sử dụng như là các đoạn mã để thực hiện. | 310 APPENDIX Coming to Objective-C from Other Languages You can think of each C object as having a pointer to an array of function pointers. When the compiler sees that the code wants to invoke a virtual function it calculates an offset from the start of the vtable emits machine code to take the function pointer at that offset from the start of the vtable and uses that as the chunk of code to execute. This process requires the compiler to know at compile time the type of the object that is calling the member function so that it can calculate the correct offset into the vtable. This kind of dispatch is very fast requiring just a couple of pointer operations and one read to get the function pointer. The Objective-C way described in detail in Chapter 3 uses a runtime function to poke around in the various class structures searching for the code to invoke. This technique can be several times slower than the C route. Objective-C adds flexibility and convenience at the expense of speed and safety which is a classic trade-off. With the C model the member function dispatch is fast. It is also very safe because the compiler and linker make sure that the object being used can handle that method. But the C method can also be inflexible because you can t really change the kind of object you re dealing with. You have to use inheritance to allow different classes of objects to react to the same message. A lot of information about a class is not retained by the C compiler such as its inheritance chain the members that compose it and so on. At runtime the ability to treat objects generically is limited. The most you can do at runtime is a dynamic cast which tells you if an object is a specific kind of subclass of another object. The C inheritance hierarchy can t be changed at runtime. Once the program has been compiled and linked it s pretty much set in stone. Dynamic loading of C libraries is frequently problematic due in part to the complexities of C name mangling the way it .

TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.