Function member of C ++ Object Model (3)

zhaozj2021-02-17  49

1.1.1. Multiple inheritance functions

0001 class Base10002 {0003 public: 0004 Base1 (); 0005 virtual ~ Base1 (); 0006 virtual void speakClearly (); 0007 virtual Base1 * clone () const; 0008 protected: 0009 float data_Base1; 0010}; 0011 class Base20012 {0013 public: 0014 Base2 (); 0015 virtual ~ Base2 (); 0016 virtual void mumble (); 0017 virtual Base2 * clone () const; 0018 protected: 0019 float data_Base2; 0020}; 0021 class Derived: public Base1, public Base20022 { 0023 public: 0024 dervied (); 0025 Virtual ~ derVied (); 0026 Virtual Dervied * Clone () const; 0027 protected; 0028 float data_dercip; 0029};

Dervied supports the difficulty of virtual function (Virtual function) is based on Base2 SubObject, there are three issues to be resolved:

1) Destructor

2) Base2 :: mumble ();

3) a set of Clone functions

Under multiple inheritance, a derived class produces N-1 additional virtual functions, used to represent the number of base classes (previous layers), and single inheritance can also be considered as special multiple inheritance. The object model is as follows:

Base1 Object Base1 Virtual Function Table

Data_base1_vptr_base1

# 0 TYPE_INFO for Base1 # 1 Base1 :: ~ Base1 () # 2 base1 :: clone () # 3 bae1 :: speakclearly ()

Base2 Object Base2 Virtual Function Table

DATA_BASE2 _VPTR_BASE2

# 0 TYPE_INFO for Base2 # 1 Base2 :: ~ Base2 () # 2 base2 :: mumble () # 3 base2 :: clone ()

# 0 TYPE_INFO for derived # 1 derived :: ~ derived () # 2 base1 :: speakclearly () # 3 derived :: clone () # 4 base2 :: mumble ()

Derived Object Derived Virtual Function Table (Shared with Base1)

Base1 Object Data_base1_vptr_base1 base2 Object data_base2_vptr_base2 data_derived

Base2_derived Virtual Function Table

# 0 TYPE_INFO for Derived # 1 derived :: ~ derived () # 2 base2 :: mumble () # 3 derived :: clone ()

When a Derived object is specified to a base1 or derived pointer, the processed Virtual Table is the primary table vtbl_derived pointer, and assigns a DeriveD object to a base2 pointer, the processed Virtual Table is the secondary table vtbl_base2_derived. There are three subsequent base classes that will affect the support of Virtual Function:

1) Call the Derived Class Virtual Function by successively Base Class Pointer. That is, the aforementioned 1)

Base2 * pb2 = new deerid;

DELETE PB2;

// C pseudo code

Derived * tmp = new deerid;

Base2 * PB2 = TEMP? TEMP SIZEOF (Base1): 0;

(* PB2-> VPTR [1]) (PB2 - SIZEOF (Base1));

2) Call the inheritance of the subsequent Base Class through the Dervied Class Pointer. That is, the aforement 2)

Derived * pb = new deerid;

PD-> MUMBLE ();

// C pseudo code

Derived * pd = new deerid;

(* PD-> VPTR [2]) ();

3) Under the nature of language expansion, the return value type of a Virtual Function may vary, possibly Base Type or may be publicly derived type. That is, the aforementioned 3)

Base2 * pb1 = new deerid;

Base2 * PB2 = PB1-> Clone ();

// C pseudo code

Derived * tmp = new deerid;

Base2 * PB1 = TEMP? TEMP SIZEOF (Base1): 0;

Derived * Temp = (* PB1-> VPTR [3]) ();

Base2 * PB2 = TEMP? TEMP SIZEOF (Base1): 0;

转载请注明原文地址:https://www.9cbs.com/read-29280.html

New Post(0)