TAILIEUCHUNG - Thinking in C plus plus (P14)

Tham khảo tài liệu 'thinking in c plus plus (p14)', 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ả | Because we can now guarantee that we know exactly what kind of objects are in the container the destructor works correctly and the ownership problem is solved - or at least one approach to the ownership problem. Here if you push a string pointer onto the StringStack then according to the semantics of the StringStack you re also passing ownership of that pointer to the StringStack If you pop the pointer you not only get the pointer but you also get ownership of that pointer. Any pointers that are left on the StringStacl when its destructor is called are then deleted by that destructor. And since these are always string pointers and the delete statement is working on string pointers instead of void pointers the proper destruction happens and everything works correctly. There is a drawback this class works only for string pointers. If you want a Stacl that works with some other kind of object you must write a new version of the class so that it works only with your new kind of object. This rapidly becomes tedious and is finally solved using templates as you will see in Chapter 16. We can make an additional observation about this example it changes the interface of the Stacl in the process of inheritance. If the interface is different then a StringStackreally isn t a Stack and you will never be able to correctly use a StringStackas a Stack. This makes the use of inheritance questionable here if you re not creating a String Stackthat is-a type of Stack then why are you inheriting A more appropriate version of StringStackwill be shown later in this chapter. Functions that don t automatically inherit Not all functions are automatically inherited from the base class into the derived class. Constructors and destructors deal with the creation and destruction of an object and they can know what to 630 Thinking in C www. BruceEckel .com do with the aspects of the object only for their particular class so all the constructors and destructors in the hierarchy below them must be

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.