Summary of the virtual function tables in C ++ and the functions

Source: Internet
Author: User

Summary of the virtual function tables in C ++ and the functions
The virtual function table is complex in the inheritance hierarchy. The important points are summarized as follows: 1. The virtual function pointer is generally at the beginning or end of the object. 2. The virtual function table is actually an array of the function pointer type. 3. How does the compiler know the virtual function table pointer? The answer is that the last element of the virtual function table is NULL4. When multiple base classes are derived, the derived class inherits the virtual function pointer of all base classes. 5. The virtual function table is class-level, all objects of the class share the same virtual function table. 6. Objects of different classes have different virtual function tables. 7. In inheritance, when a virtual function is added to the subclass, a virtual function table is not added separately. Instead, the virtual function is placed in the inherited virtual function table of the first base class.
C language problems virtual functions

The role of a virtual function is to adjust the function to the derived class through the pointer of the base class or reference.

The code above demonstrates the role of a virtual function.

You can delete all the virtual functions in this program, run the program, and observe the two results are different. It is estimated that you can understand the role of the virtual function.

Virtual functions in c/c ++

B B B = new B (); // This sentence is incorrect and cannot contain parentheses.

Your question can be explained as follows:
Class A {virtual void fun ();}
Class B: {
Virtual void fun ();
}
Int main (){
B B = new B;
B. fun (); // call B's fun ();
A * p = & B;
P-> fun (); // call B's fun ();
Return 0;
}
Reference: www.vckbase.com/document/viewdoc /? Id = 950

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.