Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
dynamic_cast (chủ đề của phần đầu tiên của chương này), const_cast, static_cast và reinterpret_cast. Cửa sổ của cơ hội này mở ra khi cần cho dynamic_cast phát sinh ý nghĩa của cú pháp đúc hiện có đã quá quá tải để hỗ trợ bất kỳ chức năng bổ sung. | Trash clone const Info info return new Glass info.data endif GLASS_H And here s a new type of Trash C09 Cardboard.h The Cardboard class with prototyping ifndef CARDBOARD_H define CARDBOARD_H include Trash.h class Cardboard public Trash static double val protected Cardboard friend class TrashPrototypeInit public Cardboard double wt Trash wt double value const return val static void value double newVal val newVal std string id return Cardboard Trash clone const Info info return new Cardboard info.data . . endif CARDBOARD_H The static val data members must be defined and initialized in a separate code file C09 TrashStatics.cpp O Contains the static definitions for the Trash type s val data members include Trash.h include Aluminum.h include Paper.h include Glass.h include Cardboard.h double Aluminum val 1.67 double Paper val 0.10 double Glass val 0.23 Chapter 16 Design Patterns 481 double Cardboard val 0.14 There s one other issue initialization of the static data members. TrashPrototypelnit must create the prototype objects and add them to the static Trash prototypes vector. So it s very important that you control the order of initialization of the static objects so the prototypes vector is created before any of the prototype objects which depend on the prior existence of prototypes. The most straightforward way to do this is to put all the definitions in a single file in the order in which you want them initialized. TrashPrototypelnit must be defined separately because it inserts the actual prototypes into the vector and throughout the chapter we ll be inheriting new types of Trash from the existing types. By making this one class in a separate file a different version can be created and linked in for the new situations leaving the rest of the code in the system alone. C09 TrashPrototypeInit.cpp o Performs initialization of all the prototypes. Create a different version of this file to make different kinds of Trash. include Trash.h include Aluminum.h include Paper.h