Thinking in c volume 1 - 2nd edition - phần 9

Bây giờ viết một chức năng mà mất như một đối số const X & và nhân bản một bản sao địa phương có thể được sửa đổi. Hạn chế tiếp cận này là bạn chịu trách nhiệm một cách rõ ràng phá hủy các đối tượng nhân bản vô tính (bằng cách sử dụng xóa) khi bạn đang thực hiện với nó. Giải thích những gì xảy ra với cả hai Mem | Not only does this save code space it allows easy propagation of changes. Pure virtual definitions It s possible to provide a definition for a pure virtual function in the base class. You re still telling the compiler not to allow objects of that abstract base class and the pure virtual functions must still be defined in derived classes in order to create objects. However there may be a common piece of code that you want some or all of the derived class definitions to call rather than duplicating that code in every function. Here s what a pure virtual definition looks like C15 Pure virtual base definitions include iostream using namespace std class Pet public virtual void speak const 0 virtual void eat const 0 Inline pure virtual definitions illegal virtual void sleep const 0 OK not defined inline void Pet eat const cout Pet eat endl void Pet speak const cout Pet speak endl class Dog public Pet public Use the common Pet code void speak const Pet speak void eat const Pet eat 684 Thinking in C www. BruceEckel .com int main Dog simba Richard s dog The slot in the Pet VTABLE is still empty but there happens to be a function by that name that you can call in the derived class. The other benefit to this feature is that it allows you to change from an ordinary virtual to a pure virtual without disturbing the existing code. This is a way for you to locate classes that don t override that virtual function. Inheritance and the VTABLE You can imagine what happens when you perform inheritance and override some of the virtual functions. The compiler creates a new VTABLE for your new class and it inserts your new function addresses using the base-class function addresses for any virtual functions you don t override. One way or another for every object that can be created that is its class has no pure virtuals there s always a full set of function addresses in the VTABLE so you ll never be able to make a call to an address that isn

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
12    362    1    16-06-2024
Đã 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.