Test the C ++ virtual function table

Source: Internet
Author: User
/* Goal, C ++ virtual function table test, test part, other circumstances will have the opportunity to learn again. Date, 2013-3-7http: // empty <iostream> Using STD: cout; Using STD: Endl; typedef void (* voidfun) (); // an empty class, see what information can be mined to compare it with other situations. Class A1 {}; // class A2 with a non-virtual function {public: void F () {cout <"A2: F () "<Endl ;}; // class A3 {public: Virtual void F () {cout <" A3: F () with one virtual function () "<Endl ;};// class A4 {public: Virtual void F2 () {cout <" A4: F2 () with two virtual functions () "<Endl;} virtual void F1 () {cout <" A4: F1 () "<Endl ;}; // single inheritance, do not overwrite the base class virtual function class A5: Public A3 {public: Virtual void g () {cout <"A5: G ()" <Endl ;}}; // single inheritance, does not cover basic class virtual functions, Level 3 class A51: Public A5 {Public: Virtual void H () {cout <"A51: H ()" <Endl ;}; // a single inheritance that covers the base class virtual function class A6: public A3 {public: Virtual void g () {cout <"A6: G ()" <Endl;} virtual void F () {cout <"A6 :: F () "<Endl ;};// multi-inheritance, does not overwrite the base class virtual function class A7: Public A3, public A4 {public: Virtual void G1 () {cout <"A7: G1 ()" <Endl;} virtual void g () {cout <"A7: G () "<Endl ;}}; // multi-inheritance, covering the class A8: Public A3, public A4 {public: Virtual void F1 () {Cout <"A8: F1 ()" <Endl;} virtual void F () {cout <"A8: F ()" <Endl ;} virtual void g () {cout <"A8: G ()" <Endl ;}}; int main () {// pointer to the table of the virtual function table. It points to the table of a virtual function table, and the element in the table points to the virtual function table. // The virtual function table consists of the virtual function addresses of the class. Int ** pvtable; // A1 A1 in case of empty classes; // although there is nothing, the size of the empty class object is 1. Why is it not 0, but 1 rather than 2? // I do not know the specific details. For the empty Class Object array a [10], the space of the empty class can be distinguished by a [0], a [1]. Cout <"Empty Class A1 size:" <sizeof (A1) <Endl; cout <"A1's virtual function table address:" <(int *) (& A1) <Endl; cout <Endl; // class A2 with a non-virtual function; // because there is no data member, the size is still 1, the member functions are stored elsewhere. Cout <"Size of a non-virtual function class A2:" <sizeof (A2) <Endl; cout <"A2's virtual function table address: "<(int *) (& A2) <Endl; cout <" Address of the 1st functions in the A2 virtual function table: "<(int *) * (int *) (& A2) <Endl; // this class has no virtual function table because no virtual function exists, therefore, an error occurs when using the following statement (the value at the unknown // address is obtained ). // (Voidfun) * (int *) (& A2) (); cout <Endl; // class A3 A3 with one virtual member function; // because of the virtual member function, one virtual function table address Member is added with a size of 4. Cout <"size of class A3 with 1 virtual function:" <sizeof (A3) <Endl; cout <"A3's virtual function table address: "<(int *) (& A3) <Endl; pvtable = (INT **) & A3; cout <" Address of the 1st functions in the A3 virtual function table: "<& pvtable [0] [0] <Endl; (voidfun) pvtable [0] [0]) (); // cout <"the address of the 2nd functions in the A3 virtual function table:" <& pvtable [0] [1] <Endl; // The call fails. // (Voidfun) pvtable [0] [1]) (); the last element of the cout <"A3 virtual function table is: "<pvtable [0] [1] <Endl; // as shown in the preceding figure, what is the content of the last element of the virtual function table? Why is it not 0? Cout <Endl; // class A4 A4 with two virtual member functions; // size or 4 cout <"class A4 with two virtual functions: "<sizeof (A4) <Endl; cout <" A4 virtual function table address: "<(int *) (& A4) <Endl; pvtable = (INT **) & A4; cout <"Address of the 1st functions in the A4 virtual function table:" <& pvtable [0] [0] <Endl; (voidfun) pvtable [0] [0]) (); Address of the 2nd functions in the cout <"A4 virtual function table: "<& pvtable [0] [1] <Endl; (voidfun) pvtable [0] [1]) (); cout <"the last element of the A4 virtual function table is:" <pvtable [0] [2] <Endl; // It is found from the above that the virtual function is really a table and is based on the class In the virtual function declaration sequence. Cout <Endl; // single inheritance, does not cover the A5 A5; cout <"single inheritance, does not cover the A5 size of the base class virtual function: "<sizeof (A5) <Endl; cout <" A5 virtual function table address: "<(int *) (& A5) <Endl; pvtable = (INT **) & A5; cout <"Address of the 1st functions in the A5 virtual function table:" <& pvtable [0] [0] <Endl; (voidfun) pvtable [0] [0]) (); Address of the 2nd functions in the virtual function table of cout <"A5: "<& pvtable [0] [1] <Endl; (voidfun) pvtable [0] [1]) (); cout <"the last element of the A5 virtual function table is:" <pvtable [0] [2] <Endl; // from the virtual function table that is sent to the derived class, the virtual function of the base class is derived Class virtual function front cout <Endl; // single inheritance Level 3, does not cover the base class virtual function A51 A51; cout <"single inheritance Level 3, the size of class A51 does not cover the basic class virtual functions: "<sizeof (A51) <Endl; cout <" A51 virtual function table address: "<(int *) (& A51) <Endl; pvtable = (INT **) & A51; cout <" Address of the 1st functions in the A51 virtual function table: "<& pvtable [0] [0] <Endl; (voidfun) pvtable [0] [0]) (); cout <"Address of the 2nd functions in the virtual function table of A51:" <& pvtable [0] [1] <Endl; (voidfun) pvtable [0] [1]) (); cout <"Address of the 3rd functions in the virtual function table of A51: "<& pvtable [0] [2] <Endl; (voi Dfun) pvtable [0] [2]) (); cout <"the content of the last element of the virtual function table of A51 is: "<pvtable [0] [3] <Endl; // The virtual function table from the derived class, the virtual function of the base class is cout <Endl; // a single inheritance before the virtual function of the derived class, covering the base class virtual function A6 A6; cout <"single inheritance, the size of the A6 class that overrides the base-class virtual functions: "<sizeof (A6) <Endl; cout <" A6's virtual function table address: "<(int *) (& A6) <Endl; pvtable = (INT **) & A6; Address of the 1st functions in the cout <"A6 virtual function table: "<& pvtable [0] [0] <Endl; (voidfun) pvtable [0] [0]) (); cout <"A6 address of the 2nd functions in the virtual function table:" <& pvtable [0] [1] <End L; (voidfun) pvtable [0] [1]) (); the last element of the virtual function table of cout <"A6 is: "<pvtable [0] [2] <Endl; // a derived class overwrites the virtual function of the base class to replace the location of the base class's virtual function cout <Endl; // multi-inheritance, without overwriting the basic class virtual function A7 A7; // because two classes are inherited, there are two virtual function tables, so the size is 8. Cout <"Multi-inheritance, does not overwrite the A7 size of the class of the base class virtual function:" <sizeof (A7) <Endl; cout <"A7 virtual function table address:" <(int *) (& A7) <Endl; pvtable = (INT **) & A7; cout <"A7 virtual function table 1's 1st function addresses:" <& pvtable [0] [0] <Endl; (voidfun) pvtable [0] [0]) (); the address of the 2nd functions in the virtual function table 1 of cout <"A7: "<& pvtable [0] [1] <Endl; (voidfun) pvtable [0] [1]) (); cout <"A7 virtual function table 1's 3rd function addresses:" <& pvtable [0] [2] <Endl; (voidfun) pvtable [0] [2]) (); the content of the last element of cout <"A7's virtual function table 1 is :" <Pvtable [0] [3] <Endl; cout <"A7 virtual function table 2 contains the addresses of the 1st functions: "<& pvtable [1] [0] <Endl; (voidfun) pvtable [1] [0]) (); cout <"the address of the 2nd functions in table 2 of the A7 virtual function:" <& pvtable [1] [1] <Endl; (voidfun) pvtable [1] [1]) (); the last element of cout <"A7's virtual function table 2 is: "<pvtable [1] [2] <Endl; // It is found that the derived class inherits several classes with virtual functions, and there are several virtual function tables. // The sequence of the table is the same as the declaration it inherits. // Place the virtual function of the derived class in the virtual function table corresponding to its first parent class. Cout <Endl; // multi-inheritance, covering the A8 A8 of the basic class virtual function; // because the two classes are inherited, there are two virtual function tables, so the size is 8. Cout <"Multi-inheritance, covering the A8 size of the base class virtual function:" <sizeof (A8) <Endl; cout <"A8 virtual function table address: "<(int *) (& A8) <Endl; pvtable = (INT **) & A8; cout <"A8 virtual function table 1's 1st function addresses:" <& pvtable [0] [0] <Endl; (voidfun) pvtable [0] [0]) (); cout <"A8 virtual function Table 1 contains the address of the 2nd functions: "<& pvtable [0] [1] <Endl; (voidfun) pvtable [0] [1]) (); cout <"A8 virtual function table 1's 3rd function addresses:" <& pvtable [0] [2] <Endl; (voidfun) pvtable [0] [2]) (); the content of the last element of cout <"A8's virtual function table 1 is:" <<Pvtable [0] [3] <Endl; cout <"A8 virtual function table 2 contains the address of the 1st functions: "<& pvtable [1] [0] <Endl; (voidfun) pvtable [1] [0]) (); cout <"A8 virtual function table 2's 2nd function addresses:" <& pvtable [1] [1] <Endl; (voidfun) pvtable [1] [1]) (); the last element of cout <"A8's virtual function table 2 is: "<pvtable [1] [2] <Endl; // It is found that all the virtual functions that the derived class overwrites the base class are stored in Table 1 of the virtual function, place them in the declared inheritance order. Cout <Endl; return 0;}/* does not copy the result. Analyze the results by yourself based on the main program. */

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.