The principle of C + + virtual function

Source: Internet
Author: User

The member functions in a class are divided into static member functions and non-static member functions, and non-static member functions are divided into ordinary and virtual functions.

Q: Why virtual functions are used

A: With virtual functions, we can get good scalability . In a well-designed object-oriented program, most functions communicate with the interface of the base class. Because the base class interface is used, the program that calls the base class interface can adapt to the new class without changing it. If a user wants to add a new feature, he can inherit from the base class and add the appropriate new functionality.

Q: Brief Introduction to C + + virtual function and underlying implementation principle

A: The main point is to answer the virtual function table and virtual function table pointer function.

Virtual functions in C + + are implemented using virtual function tables and virtual function table pointers. The virtual function table is the Address table of a virtual function of a class, used to index the class itself and the address of the virtual function of the parent class, and if the subclass overrides the virtual function of the parent class, then the corresponding virtual function in the virtual function table is replaced with the address of the function of the subclass (the subclass may not be virtual function, but must have the same name); The virtual function table pointer exists in each object (usually at the start address of an object for efficiency), pointing to the address of the virtual function table of the class in which the object resides, and in multiple-inheritance environments, there are multiple virtual function table pointers. Points to the virtual function table corresponding to the different base classes, respectively.

--------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------

A virtual function table is one for each class that has a virtual function. A virtual function table Pointer is a corresponding one for each object .

Ref

http://blog.csdn.net/haoel/article/details/1948051

http://songlee24.github.io/2014/09/02/cpp-virtual-table/

The principle of C + + 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.