The basic principle of realization of virtual function of C + + 's essays (text) _c language

Source: Internet
Author: User

1. Overview

Simply put, each class that contains a virtual function, whether it is itself or inherited, has at least one corresponding virtual function table, which holds the corresponding function pointers for all the virtual functions of the class. Cases:

which

The virtual function table in B holds the B::foo and b::bar two function pointers.
In the virtual function table of D, there are both the virtual function B::foo inherited from B, the d::bar of the base class virtual function B::bar, and the new virtual function D::quz.

Tip: In order to describe the convenience, this article ignores the effect of the memory alignment on the layout when discussing the memory layout of the object.

2. virtual function Table construction process

From the compiler's point of view, B's virtual function table is well constructed, and D's virtual function table construction process is relatively complex. The following is a way to construct a virtual function table for D (for reference purposes only):

Tip: This process is done by the compiler, so it can also be said that the virtual function substitution process occurs at compile time.

3. virtual function Call procedure

Take the following procedure as an example:

The compiler only knows that a PB is a pointer to a b* type and does not know the specific object type it points to: PB may point to an object of B, or it may point to an object of D.

But for "Pb->bar ()", compile-time can determine that another parameter here operator-> is B::bar (because PB is b* type, the compiler thinks bar is B::bar), and B::bar and D:: Bar is the same offset position in the respective virtual function table.

No matter what type of object the PB points to, as long as you can determine the offset value of the called function in the virtual function, when you are ready to run, you can determine the specific type, and can find the corresponding vptr, you can find the actual function should be called.

Hint: I have mentioned in the article "C + + notes: Deep understanding of data member pointers, function member pointers": The PTR portion of the virtual function pointer is the offset value in the virtual function table (in bytes) plus 1.

B::bar is a virtual function pointer, which has a PTR portion of 9, and its offset value in the virtual function table of B is 8 (8+1=9).

When the program executes to "Pb->bar ()", it is already able to determine the specific types of PB points:

Copy Code code as follows:

If the PB points to B's object, you can get the vptr of the B object, plus the offset value 8 ((char*) vptr + 8) to find B::bar.
If the PB points to the object of D, you can get the vptr of the D object, plus the offset value 8 ((char*) vptr + 8) to find D::bar.
If PB points to other types of objects ... Similarly...

4. Multiple inheritance

When a class inherits multiple classes, and multiple base classes have virtual functions, the subclass object contains pointers to multiple virtual function tables (that is, multiple vptr), for example:

Wherein: D's own virtual function shares the same virtual function table as the B-base class, so it is also called B as the primary base class for D (primary base class).

The virtual function substitution process is similar to the previous description, except that a virtual function table is more than one copy-and-replace process.

The call procedure for a virtual function is basically similar to the previous description, except that the location that the base class pointer points to may not be the starting position of the derived class object, as an example of the following program:

5. Diamond Inheritance

This article does not discuss the case of diamond inheritance, personal feel: The complexity of diamond inheritance is much greater than its use value, this is the C + + people love and hate one of the reasons.

If you want to delve into it, you can refer to the Itanium C + + ABI.

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.