Under what circumstances should class destructor be declared as virtual functions?

Source: Internet
Author: User
Destructor usage:
1. Each destructor (without virtual) is only responsible for clearing its own members. 2. There may be a base class pointer pointing to the case where it is indeed a member of a derived class.

For the second case:

Correct execution: The base class pointer can point to the object (polymorphism) of the derived class. If you delete this pointer [] P;Call the derived class destructor to which the Pointer PointsThe destructor of the derived classAutomatically calls the destructor of the base classIn this way, the entire derived class object is completely released.
If the Destructor is not declared as a virtual function, the compiler implements static binding. When the base class pointer is deleted,Only the base class destructor are called, but not the derived class destructor.In this way, the structure of the derived class object is incomplete. Therefore, it is necessary to declare the Destructor as a virtual function.
Therefore, to run an appropriate destructor, The destructor in the base class must be a virtual destructor. When you write a class, writing its destructor as a virtual function will not be wrong, and it will be reloaded.
Example:
Assume that the parent class is a and the subclass is B. When a * P = new B ();
If the destructor in A is not virtual, delete p is called and B's destructor is not called, in this way, the resources allocated in B cannot be released.
Memory leakage
Class
{
Public:
Virtual ~ A (void );
};

Class B: public
{
Public:
Virtual ~ B (void );
Int N;
};

If you use:

A * pb = new B;
Then
Delete Pb;
If the destructor of A is not virtual, when the pointer Pb of A is deleted, only the Destructor called by a isBut if the destructor of A is virtual, the True Type of destructor of Pb will be searched from the virtual function table in Pb When PB is deleted.

If the Destructor are not virtual, only the structure of Class A is called When PB is deleted, but the structure of Class B is not called, so int N in Class B will not be deleted, causing memory leakage!

Note:

1. after a member function is declared as a virtual function, classes in the same type of family cannot define a non-virtual but have the same parameters (including the number and type) as the virtual function) a function of the same name as the function return value type.

2. What are the considerations for declaring a member function as a virtual function?

(1) Whether the class where the member function is located serves as the base class.

(2) Is it possible for a member function to be changed after the class is inherited? If you want to change its function, you should declare it as a virtual function.

If a member function does not need to be modified after the class is inherited, or the derived class does not use this function, do not declare it as a virtual function.

Do not declare all member functions in the class as virtual functions only for the basis class.

(3) Whether the call is accessed through the object name or through a base class pointer or reference. If it is accessed through a base class pointer or reference, it should be declared as a virtual function.

Note: When using virtual functions, the system requires a certain amount of space overhead. When a class has a virtual function, the compilation system constructs a virtual function table for the class, which is a pointer array that stores the entry address of each virtual function. The cost of the system for Dynamic Association is very small, improving the efficiency of polymorphism.

3. The role of the Destructor is to undo the class object from the memory before the object is revoked. Generally, the system only executes the destructor of the base class and does not execute the destructor of the derived class.

You only need to declare the destructor of the base class as a virtual function, that is, the virtual destructor. In this way, the base class object is revoked and the object of the derived class is revoked, this process is completed dynamically.

4. if you declare the destructor of the base class as a virtual function, the destructor of all the derived classes of the base class automatically become virtual functions, even if the name of the derived class's destructor is different from that of the base class.

It is best to declare the destructor of the base class as a virtual function. This will make the destructor of all derived classes automatically become virtual functions. If the explicit Delete operator in the program deletes an object, and the operation object uses the base class pointer pointing to the object of the derived class, the system calls the destructor of the corresponding class.

5.Constructors cannot be declared as virtual functions.

6. Pure virtual functions

Sometimes, the virtual functions in the base class are declared and defined for use in the derived class, which has no meaning in the base class. These functions are called pure virtual functions. They do not need to be written as empty functions, but must be declared as follows:

Virtual float area () const = 0;

General Format: virtual function type function name (table of parameters) = 0;

Note:Pure virtual functions do not have function bodies;

"= 0" does not mean that the return value of the function is 0. It only serves the form and tells the compilation system. "This is a pure virtual function ";

This is a declaration statement and should end with a semicolon.

Pure virtual functions only have function names but do not have function functions and cannot be called. This function can be called only when it is defined in a derived class.

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.