TAILIEUCHUNG - A Complete Guide to Programming in C++ part 58

A Complete Guide to Programming in C++ part 58. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | DESTROYING DYNAMICALLY ALLOCATED OBJECTS 549 Dynamically created objects in a class hierarchy are normally handled by a base class pointer. When such an object reaches the end of its lifetime the memory occupied by the object must be released by a delete statement. Example Car carPtr carPtr new PassCar 500 false 21 Geo delete carPtr Destructor Calls When memory is released the destructor for an object is automatically called. If multiple constructors were called to create the object the corresponding destructors are called in reverse order. What does this mean for objects in derived classes The destructor of the derived class is called first and then the destructor of the base class executed. If you use a base class pointer to manage an object the appropriate virtual methods of the derived class are called. However non-virtual methods will always execute the base class version. In the previous example only the base class destructor for Car was executed. As the PassCar destructor is not called neither is the destructor called for the data member passCarType which is additionally defined in the derived class. The data member passCarType is a string however and occupies dynamically allocated memory this memory will not be released. If multiple objects are created dynamically in the derived class a dangerous situation occurs. More and more unreferenced memory blocks will clutter up the main memory without you being able to reallocate them this can seriously impact your program s response and even lead to external memory being swapped in. Virtual Destructors This issue can be solved simply by declaring virtual destructors. The opposite page shows how you would define a virtual destructor for the Car class. Just like any other virtual method the appropriate version of the destructor will be executed. The destructors from any direct or indirect base class then follow. A class used as a base class for other classes should always have a virtual destructor defined. Even if .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.