Virtual function in C ++

Source: Internet
Author: User

After listening to the C ++ class, I thought it was really good to remember. Write something when you think of it. (PS: The first time I had to brush the night, I was a little excited. O (∩ _ ∩) O)

Inheritance first, the relationship between is-a in many real-world languages cannot be used as a criterion in programming. Specifically, we need to meet the Rys replacement principle =, that is, the sub-class has various attributes of the parent class, and the sub-class must implement all the methods of the parent class. Cannot be undone.

For example, if the parent class A has a non-virtual function method void F (), then subclass B inherits subclass A and overwrites F (); then a A = new B; then B. F () is to call the F () method in. If the F method is defined as virtual function. Then there is no such problem, because in this process, the compiler sees that the subclass inherits the parent class, and then uses dynamic binding when the new subclass is used. Then OK.

The first version of the Destructor is a virtual function, because the data in the object of the subclass is usually more than that in the parent class. So, if it is not a virtual function or a static binding, you can only delete the data part of the parent class. The memory space cannot be completely released.

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.