TAILIEUCHUNG - Absolute C++ (4th Edition) part 44

Absolute C++ (4th Edition) part 44. KEY BENEFIT: C++ programming concepts and techniques are presented in a straightforward style using understandable language and code. KEY TOPICS: C++ Basics; Flow of Control; Function Basics; Parameters and Overloading; Arrays; Structures and Classes; Constructors; Operator Overloading, Friends, and References; Strings; Pointers and Dynamic Arrays; Separate Compilation and Namespaces; Streams and File I/O; Recursion; Inheritance; Polymorphism and Virtual Functions; Templates; Linked Data Structures; Exception Handling; Standard Template Library; Patterns and UML. MARKET: Useful for both beginning and intermediate C++ programmers. . | 436 Pointers and Dynamic Arrays class StringClass public void someProcessing StringClass operator const StringClass rtSide private char a Dynamic array for characters in the string int capacity size of dynamic array a int length Number of characters in a must be a member As noted in Chapter 8 when you overload the assignment operator it must be a member of the class it cannot be a friend of the class. That is why the above definition has only one parameter for operator. For example consider the following si s2 s1 and s2 in the class StringClass calling object for In the above call si is the calling object and s2 is the argument to the member operator . The following definition of the overloaded assignment operator can be used in chains of assignments like si s2 s3 and can be used to invoke member functions as follows si s2 .someProcessing The definition of the overloaded assignment operator uses the this pointer to return the object on the left side of the sign which is the calling object This version does not work in all cases. StringClass StringClass operator const StringClass rtSide capacity length delete a a new char capacity for int i 0 i length i a i i return this This version has a problem when used in an assignment with the same object on both sides of the assignment operator like the following s s Classes Pointers and Dynamic Arrays 437 When this assignment is executed the following statement is executed delete a But the calling object is s so this means delete The pointer is then undefined. The assignment operator has corrupted the object s and this run of the program is probably ruined. For many classes the obvious definition for overloading the assignment operator does not work correctly when the same object is on both sides of the assignment operator. You should always check this case and be careful to write your definition of the overloaded assignment operator so that it also works in this case. To avoid

TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
28    152    1    23-11-2024
5    163    1    23-11-2024
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.