Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Absolute C++ (4th Edition) part 45. 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. . | 446 Pointers and Dynamic Arrays destructor name object from a class with a destructor then when the function call ends the destructor will be called automatically. If the destructor is defined correctly the destructor will call delete to eliminate all the dynamically allocated variables created by the object. This may be done with a single call to delete or it may require several calls to delete. You may also want your destructor to perform some other clean-up details as well but returning memory to the freestore manager for reuse is the main job of the destructor. The member function PFArrayD is the destructor for the class PFArrayD shown in Display 10.10. Like a constructor a destructor always has the same name as the class of which it is a member but the destructor has the tilde symbol at the beginning of its name so you can tell that it is a destructor and not a constructor . Like a constructor a destructor has no type for the value returned not even the type void. A destructor has no parameters. Thus a class can have only one destructor you cannot overload the destructor for a class. Otherwise a destructor is defined just like any other member function. Notice the definition of the destructor PFArrayD given in Display 10.11. PFAr-rayD calls delete to eliminate the dynamically allocated array pointed to by the member pointer variable a. Look again at the function testPFArrayD in the sample program shown in Display 10.12. The local variable temp contains a dynamic array pointed to by the member variable temp.a. If this class did not have a destructor then after the call to testPFArrayD ended this dynamic array would still be occupying memory even though the dynamic array is useless to the program. Moreover every iteration of the do-while loop would produce another useless dynamic array to clutter up memory. If the loop is iterated enough times the function calls could consume all the memory in the freestore manager and your program would then end abnormally. .