Function member of C ++ object model (4)

zhaozj2021-02-17  56

1.1.1. Virtual member function under virtual inheritance

0001 Class Point2D0002 {0003 public: 0004 POINT2D (Float x = 0.00, float y = 0.00); 0005 Virtual ~ Point2d (); 0007 Virtual void Mumble (); 0007 Virtual float z () {return 0;}; 0008 protected: 0009 float _x, _y; 0010}; 0011 class Point3d: public virtual Point2d0012 {0013 public: 0014 Point3d (float x = 0.00, float y = 0.00, float z = 0.00); 0015 virtual ~ Point3d (); 0016 virtual float Z () {RETURN _Z}; 0017 protected: 0018 float _z; 0019};

The object model is as follows:

Point2D Object Virtual Table Point2D

_X _Y VPTR_2D

# 0 TYPE_INFO for Point2D # 1 Point2D :: ~ Point2D () # 2 Point2d :: Mumble () # 3 point2d :: z ()

Point3D Object Virtual Table Point3D

_Z vPTR_3D _x _y vptr_2d

# 0 TYPE_INFO for Point3D # 1 Point3d :: ~ Point3d () # 2 Point2d :: Mumble () # 3 point3d :: z ()

Virtual Table Point2D SubObject for Point3d

# 0 TYPE_INFO for Point3D # 1 Point3d :: ~ Point3d () # 2 Point2d :: Mumble () # 3 point3d :: z ()

As can be seen from the above model, virtual inheritance of virtual function calls its pointer adjustment extremely complicated, so in general, I recommend nothing to declare a non-static data member in a virtual basis to reduce the complexity of the pointer adjustment.

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

New Post(0)