ANSI/ISO C++ Professional Programmer's Handbook phần 2

Tham khảo tài liệu 'ansi/iso c++ professional programmer's handbook phần 2', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | ANSI ISO C Professional Programmer s Handbook - Chapter 2 - Standard Briefing The Latest Addenda to ANSI ISO C long n 1000000L long pl n unsigned char pc reinterpret_cast unsigned char pl printf d d d d pc 0 pc 1 pc 2 pc 3 memory dump reinterpret_cast can cast integers to pointers and vice versa. For example void pv reinterpret_cast void 0x00fffd int ptr reinterpret_cast int pv reinterpret_cast can also be used for conversions between different types of function pointers. The result of using the resultant pointer to call a function with a nonmatching type is undefined. Do not use reinterpret_cast instead of static_cast -- the results might be undefined. For example using reinterpret_cast to navigate through the class hierarchy of a multiply-inherited object is likely to yield the wrong result. Consider the following class A private int n class B private char c class C public A public B void func B pb C pc1 static_cast C pb correct offset adjustment C pc2 reinterpret_cast C pb no offset calculated int main B b func b On my machine pc1 is assigned the value 0x0064fdf0 whereas pc2 is assigned 0x0064fdf4. This demonstrates the difference between the two cast operators. Using the information that is available at compile time static_cast converts a pointer to B to a pointer to C. It does so by causing pc1 to point at the start of C by subtracting the offset of the subobject B. On the other hand reinterpret_cast simply assigns the binary value of pb to pc2 without any further adjustments for this reason it yields the wrong result. dynamic_cast In pre-standard C as was noted earlier C-style cast was used to perform a dynamic cast as well. The cast was either static or dynamic depending on the type of the operand. The Standardization committee however opposed this approach. An expensive runtime operation that looked exactly like a static cast that is penalty-free can mislead the users. For this purpose a new operator was introduced to the language dynamic_cast dynamic_cast

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã 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.