The Destructor is declared as a virtual function.

Source: Internet
Author: User
Tags types of functions

 

Base * pb; inherit C; // inherit is the Pb that inherits the base = & C; Delete Pb; the object's destructor must be called. If the base class destructor are not virtual, the destructor of the corresponding class is called according to the definition type of Pb, that is, the class structure. However, if you have a memory release operation in the derived class structure, memory leakage occurs. If the base class destructor is virtual, the corresponding class destructor will be called based on the type of the object referred to by Pb, that is, the derived class destructor, the derived class destructor is called based on the hierarchy principle of the Destructor call. In this way, there will be no problems. Negative tive C ++ (article 7th: declare the destructor of the polymorphism base class as a virtual function) This is because C ++ has made such a provision: when a derived class object is deleted by a pointer to the base class and the base class has a non-virtual destructor, the result is unpredictable. In general, the newly derived parts in the derived class cannot be destroyed at runtime. If a class does not contain a virtual function, it usually means that it will not be used as a base class. When a class is not used as a base class, it is usually not a good idea to declare its destructor as virtual. The implementation of a virtual function requires that its object contain additional information, this information is used to determine the virtual function that the object needs to call at runtime. Generally, this information takes the form of a pointer, which is called "vptr" ("virtual function table Pointer "). Vptr points to an array containing function pointers. This array is called "vtbl" ("virtual function table"). Each class containing virtual functions has a vtbl associated with it. When a virtual function is called by an object, the vtbl pointed to by the vptr of the object is used. Find a proper function pointer in vtbl and call the corresponding real function. You should remember to declare the virtual destructor for the polymorphism base class. Once a class contains a virtual function, it should contain a virtual destructor. If a class does not have to be a base class or has no polymorphism, it should not be declared as a virtual destructor. I don't understand at all: why can I execute the destructor of a derived class after the destructor in the base class is defined as a virtual function? I guess: When PB = & C, the virtual function table pointer of its derived class will be copied to the base class Pb, so the destructor of its derived class will be called, then, call the destructor of the base class.
It should be noted that when the destructor of the base class is declared as a virtual function, the destructor of all the derived classes of the base class, whether or not there is virtual keyword modification, are automatically declared as virtual functions. In my understanding, there are two situations in which the virtual destructor needs to be defined. 1. If there is a virtual function in the base class, otherwise, as said, the structure of the derived class is incomplete if the base class pointer is used for analysis.
2. If the derived class customizes the operator Delete () function, virtual destructor are required no matter whether there are virtual functions in the base class. Otherwise, the structure will be analyzed in a way you do not want. Internal Mechanism of virtual functions So why can the virtual functions in C ++ implement the above Dynamic Association? In fact, the internal implementation mechanism of virtual functions is relatively complicated, and the implementation methods of different C ++ compilers are different. However, most C ++ compilers use the vtable structure. vtable is called a virtual table. Simply put, when Program During compilation, A vtable is created for each class (base class and derived class) where the virtual function is located, and the virtual function of the class is stored in this table, then, create a pointer vptr pointing to the virtual table structure vtable. vptr is called a virtual pointer. When the program runs, the vtable and vptr of the derived class object are copied to the base class based on the derived class object obtained by the Base Class Object and called by the base class, this enables Dynamic Association of class member functions. It can be seen that the address of the derived class object needs to be specified during dynamic concatenation. Therefore, the dynamic concatenation mechanism of the virtual function can be activated only through the base class pointer or reference object. It should be noted that the virtual functions to be rewritten in the derived class should be exactly the same as the virtual functions in the base class, including the return value type of the virtual functions. In ANSI/iso c ++, for a common function, the type of the function return value cannot be used as an overload for a general function to distinguish content. But for a virtual function, different types of functions return values are considered as two different virtual functions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.