Podstrony
|
[ Pobierz całość w formacie PDF ]
with their qualified name, ignoring any possible virtual overriding destructors in more derived classes. For example #include using namespace std; class A { public: virtual ~A() { cout }; class B: public A { public: ~B() { cout }; int main() { B b; return 0; }; This program displays destroying B destroying A This is because the compiler transforms the user-defined destructor of class B into ~B() { //user-written code below cout //pseudo C++ code inserted by the compiler below file:///D|/Cool Stuff/old/ftp/1/1/ch04/ch04.htm (21 von 24) [12.05.2000 14:46:07] ANSI/ISO C++ Professional Programmer's Handbook - 4 - Special Mem...nstructor, Copy Constructor, Destructor, And Assignment Operator this->A::~A(); // destructor called using its qualified name } Although the destructor of class A is virtual, the qualified call that is inserted into the destructor of class B is resolved statically (calling a function with a qualified name bypasses the dynamic binding mechanism). Explicit Destructor Invocation Destructors are invoked implicitly in the following cases: For static objects at program termination For local objects when the block in which the object is created exits For a temporary object when the lifetime of the temporary object ends For objects allocated on the free store using new, through the use of delete During stack unwinding that results from an exception A destructor can also be invoked explicitly. For example: class C { public: ~C() {} }; void destroy(C& c) { c.C::~C(); //explicit destructor activation } A destructor can also be explicitly invoked from within a member function of its object: void C::destroy() { this->C::~C(); } In particular, explicit destructor invocation is necessary for objects that were created by the placement new operator (placement new is discussed in Chapter 11, "Memory Management"). Pseudo Destructors Fundamental types have constructors, as you have seen. In addition, fundamental types also have a pseudo destructor. A pseudo destructor is a syntactic construct whose sole purpose is to satisfy the need of generic algorithms and containers. It is a no-op code that has no real effect on its object. If you examine the assembly code that your compiler produces for a pseudo destructor invocation, you might discover that the compiler simply ignored it. A pseudo destructor invocation is shown in the following example: typedef int N; int main() { N i = 0; i.N::~N(); //pseudo destructor invocation file:///D|/Cool Stuff/old/ftp/1/1/ch04/ch04.htm (22 von 24) [12.05.2000 14:46:07] ANSI/ISO C++ Professional Programmer's Handbook - 4 - Special Mem...nstructor, Copy Constructor, Destructor, And Assignment Operator i = 1; //i was not affected by the invocation of the pseudo destructor return 0; } The variable i is defined and initialized. In the following statement, the pseudo destructor of the non-class type N is explicitly invoked but it has no effect on its object. Like the constructors of fundamental types, pseudo destructors enable the user to write code without having to know if a destructor actually exists for a given type. Pure Virtual Destructors Unlike ordinary member functions, a virtual destructor is not overridden when it is redefined in a derived class. Rather, it is extended. The lower-most destructor first invokes the destructor of its base class; only then is it executed. Consequently, when you try to declare a pure virtual destructor, you might encounter compilation errors, or worse -- a runtime crash. In this respect, pure virtual destructors are exceptional among pure virtual functions -- they have to be defined. You can refrain from declaring a destructor with the pure specifier, making it only virtual. However, this is an unnecessary design compromise. You can enjoy both worlds by forcing an interface whose members are all pure virtual, including the destructor -- and all this without experiencing runtime crashes. How is it done? First, the abstract class contains only a declaration of a pure virtual destructor: class Interface { public: virtual void Open() = 0; virtual ~Interface() = 0; }; Somewhere outside the class declaration, the pure virtual destructor has to be defined as follows: Interface::~Interface() {} //definition of a pure virtual destructor; should always be empty Constructors And Destructors Should Be Minimal When you are designing a class, remember that it might serve as a base for other subclasses. It can also be used as a member object in a larger class. As opposed to ordinary member functions, which can be overridden or simply not called, the base class constructor and destructor are automatically invoked. It is not a good idea to force users of a derived and embedded object to pay for what they do not need, but are forced to accept. In other words, constructors and destructors should contain nothing but the minimal functionality needed to construct an object and destroy it. A concrete example can demonstrate that: A string class that supports serialization should not open/create a file in its constructor. Such operations need to be left to a dedicated member function. When a new derived class -- such as ShortString, which holds a fixed length string -- is created, its constructor is not forced to perform superfluous file I/O that is imposed by the constructor of its base class. Conclusions The constructor, copy constructor, assignment operator, and destructor automate most of the tedium that is associated with creating, copying, and destroying objects. The symmetry between a constructor and a destructor in C++ is rare among object-oriented programming languages, and it serves as the basis for advanced design idioms (as you will see in the next chapter, "Object Oriented Programming and Design"). Each C++ object possesses the four member functions, which can be declared by the programmer or declared implicitly file:///D|/Cool Stuff/old/ftp/1/1/ch04/ch04.htm (23 von 24) [12.05.2000 14:46:07] ANSI/ISO C++ Professional Programmer's Handbook - 4 - Special Mem...nstructor, Copy Constructor, Destructor, And Assignment Operator by the implementation. An implicitly-declared special member function can be trivial, which means that the implementation does not have to define it. The synthesized special member functions perform only operations that are required by the implementation. User-written special member functions are automatically augmented by the compiler -- to ensure the proper initialization of base and embedded subobjects -- and the virtual pointer. Fundamental types have constructors and pseudo destructors, which facilitate generic programming. In many cases, the synthesized special member functions do the "right thing". When the default behavior is unfitted, the programmer has to define one or more of the special functions explicitly. Often, however, the need for user-written code derives from combining low-level data structures with a high-level interface, and might indicate a design flaw. Declaring a constructor explicit ensures that it will not serve as an implicit conversion operator. A mem-initialization list is necessary for the initialization of const and reference data members, as well as to pass arguments to a base or embedded subobject. In all other cases, a mem-initialization list is optional but can enhance performance. Constructors and assignment operators can be used in several ways to control instantiation and copying of objects. Destructors can be invoked explicitly. Destructors that are declared pure virtual have to be defined. Contents © Copyright 1999, Macmillan Computer Publishing. All rights reserved. file:///D|/Cool Stuff/old/ftp/1/1/ch04/ch04.htm (24 von 24) [12.05.2000 14:46:07] ANSI/ISO C++ Professional Programmer's Handbook - Chapter 5 - Object-Oriented Programming and Design ANSI/ISO C++ Professional Programmer's Handbook Contents 5 Object-Oriented Programming and Design by Danny Kalev Introduction Programming Paradigms Procedural Programming Object-Based Programming Object-Oriented Programming Techniques Of Object-Oriented Programming Class Design
[ Pobierz całość w formacie PDF ]
zanotowane.pldoc.pisz.plpdf.pisz.plkskarol.keep.pl
|