The understanding of "virtual" in virtual function

Source: Internet
Author: User

1, first review the concept of virtual function

Virtual function: Does not mean that a subclass must be implemented, but this function of the subclass can be called by a pointer to the base class.

Pure virtual function: A subclass must be implemented, it defines only a set of behaviors, the class with pure virtual function is an abstract class, can only be used as a base class, cannot be instantiated.

2, why is called "virtual" function?

It is the unpredictability of this function call, which is not deterministic in the compile phase, and the actual address of the function call is not determined until it is executed.

3, why the destructor of a class as a base class must be a "virtual function"?

The destructor of the derived class is called first, and then the destructor of the base class is called. If the destructor is not a virtual function, and the program executes it is also

By destroying the dynamic object of the derived class with a pointer to the base class, only the destructor of the base class is called when the object is destroyed with delete, and the destructor of the derived class is not called

Function. This can result in incomplete destruction of objects and memory leaks.

The understanding of "virtual" in virtual function

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.