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

Bài tập này tạo ra một sự thay thế để sử dụng các copyconstructor. Tạo một lớp X và tuyên bố (nhưng không xác định) một bản sao tin-xây dựng. Tạo một bản sao công cộng () chức năng như một chức năng thành viên const trả về một bản sao của đối tượng được tạo ra bằng cách sử dụng mới | Another issue is heap fragmentation. By allocating objects of different sizes it s possible to break up the heap so that you effectively run out of storage. That is the storage might be available but because of fragmentation no piece is big enough to satisfy your needs. By creating your own allocator for a particular class you can ensure this never happens. In embedded and real-time systems a program may have to run for a very long time with restricted resources. Such a system may also require that memory allocation always take the same amount of time and there s no allowance for heap exhaustion or fragmentation. A custom memory allocator is the solution otherwise programmers will avoid using new and delete altogether in such cases and miss out on a valuable C asset. When you overload operator newand operator delete it s important to remember that you re changing only the way raw storage is allocated. The compiler will simply call your new instead of the default version to allocate storage then call the constructor for that storage. So although the compiler allocates storage and calls the constructor when it sees new all you can change when you overload new is the storage allocation portion. delete has a similar limitation. When you overload operatornew you also replace the behavior when it runs out of memory so you must decide what to do in your operator new return zero write a loop to call the new-handler and retry allocation or typically throw a bad_alloc exception discussed in Volume 2 available at . Overloading new and delete is like overloading any other operator. However you have a choice of overloading the global allocator or using a different allocator for a particular class. 596 Thinking in C www. BruceEckel .com Overloading global new delete This is the drastic approach when the global versions of new and delete are unsatisfactory for the whole system. If you overload the global versions you make the defaults completely inaccessible .

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
Đã 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.