C + + Object memory Model 2 (virtual function, virtual pointer, virtual function table)

Source: Internet
Author: User

From the example, examine the object memory model of the class with the virtual function as follows:

1 classA {2  Public:3     Virtual voidvfunc1 ();4     Virtual voidVfunc2 ();5     voidfunc1 ();6     voidFunc2 ();7     Virtual~A ();8 Private:9     intm_data1, M_data2;Ten };  One  A classb:a { -  Public: -     Virtual voidvfunc1 (); the     voidFunc2 (); -     Virtual~B (); - Private: -     intm_data3; + }; -  + classc:b { A  Public: at     Virtual voidvfunc1 (); -     voidfunc (); - Private: -     intm_data1, m_data4; -};

Note: It is not a best practice to have variables and non-virtual functions that have the same name as the parent class in a subclass, here to illustrate their memory structure.

See its object memory structure.

* Image from Houtie teacher

The analysis is as follows:

1. Each class containing a virtual function has one more pointer (vptr) in memory, as shown in the first position in the A,b,c object in the figure, where the virtual function table (VTBL) is stored.

2. The Virtual function table (VTBL) stores the locations of all virtual functions, and because of their dynamic binding properties, the virtual function locations stored in subclasses after overwrite (override) are not the same as those in the parent class.

3. Analyze the above code, b inherits a, so the data part of a is also inherited by B, and B adds its own data part m_data3, plus vptr, which makes up the memory layout on the left side of B.

The virtual function vfunc1 () in A, VFUNC2 () can be overwritten and dynamically bound.

So in B, vfunc1 () is overwritten, and the corresponding item in its VTBL points to the position of the new function ( bright blue ). VFUNC2 () is not overwritten and still points to the original position ( dark blue ).

C and B in the same vein, VFUNC1 () is overwritten, and the corresponding item in its VTBL points to the position of the new function ( orange color ). VFUNC2 () is not overwritten and still points to the original position ( dark blue ).

A non-virtual function is statically bound, stored in a separate memory space (Code memory section, gray function part), and called when the object's this pointer is passed to a invisible parameter to determine who is calling the function.

4. The statement that calls the virtual function is shown in the lower part of the C language, where n indicates that the corresponding function is in the first place (known to the compiler when creating the virtual function table), thus enabling dynamic binding.

C + + Object memory Model 2 (virtual function, virtual pointer, virtual function table)

Related Article

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.