c ++ model of memory: virtual functions papers

A virtual function

1. Analysis of the position of the virtual function table

    Categories: virtual functions, this class will generate a virtual function table

    Object classes: a pointer (the vptr) class virtual function table will point to the - virtual function table pointer. (The position at the beginning of the class may be memory space may also be at the end, embodied by the compiler determined)

 

2. Under the manual call virtual functions of the role of inheritance

    To get the address of the virtual function table pointer by defining the function assignments of the virtual function call can be directly and. Subclass virtual function overrides virtual functions of the parent class.

 

3. Analysis vtable

    (1) containing only a class virtual function table will exist virtual function, objects belonging to the same class share a virtual function table, but has its own virtual function table pointer (vptr). Of course, all objects of that class vptr point to the same virtual function table.

    (2) the parent class has virtual functions is equal subclasses has virtual functions. (Parent class has a virtual function table is a subclass must have virtual function tables) because inherit the parent class subclasses.

         If the virtual parent class will subclass removed virtual function, the function is no longer a virtual function?

         In the parent class is a virtual function, even if the subclass function does not write virtual, it is still virtual.

         Whether the parent class or subclass, each class have only their own unique virtual function tables. Whether there will be more virtual function tables subclass?

    Content (3) If no new virtual function subclass, the subclass vtable parent class to the content of the same virtual function table. But the positions of the two virtual function table in the memory is different. Two virtual function tables. Each virtual function comprising a first address of the virtual function table, but if the virtual function table subclasses and superclasses an vtable an represent the same function (which indicates the subclass does not cover the parent class virtual function ) function is the same as the entry points to.

    (4) beyond the virtual function table part unknown.

    (5) when the object subclass object as a parent class assignment, subclass belonging to the parent class that is part of the compiler to automatically distinguish (cut) and copy it to the parent object; so BaseClass base = derive; actually did two things: First, a base object is generated; II part derive values ​​were initialized to base objects. (But this time when the compiler virtual function table base pointer value of the processing object, the compiler will automatically according to the type of base decisions for the virtual function table address BaseClass)

    (4) OO (object-oriented) and OB (object-based) concepts

      C ++ is to support polymorphism through pointers and references class, which is a programming style, this is what we often say that the object-oriented. object-oriented model

      OB (object-base), also known as abstract data models ADT [abstract datatype model], does not support multi-state, faster execution. Because the decision (no polymorphism) analysis function calls need not run, such pbase = new new Derive * Base (); Base & Base2 = derive2; instead done during compilation, memory space is more compact compact extent, since no imaginary these concepts function.

      But poor on OB (object-base) design flexibility, C ++ supports both object-oriented programming. Also supports object-based design program.

 

4. Analysis of multiple inheritance vtable

 

Guess you like

Origin www.cnblogs.com/sxgloverr1314/p/11410039.html