1 / 22

C++ Object Model

C++ Object Model. 台大資工多媒體實驗室 林昂賢. Preview. What is C++ object model ? Class and object data member access single object layout Inheritance object layout polymorphism virtual. C++ object model. C++ compiler 底層的實做機制,用來實做各物件的運作模型,使之能具備 C++ 諸多物件導向的性質。 物件模型底層的實做機制並未標準化,各家 compiler 作法不一。

zora
Télécharger la présentation

C++ Object Model

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Object Model 台大資工多媒體實驗室 林昂賢

  2. Preview • What is C++ object model ? • Class and object • data member access • single object layout • Inheritance object layout • polymorphism • virtual

  3. C++ object model • C++ compiler底層的實做機制,用來實做各物件的運作模型,使之能具備C++諸多物件導向的性質。 • 物件模型底層的實做機制並未標準化,各家compiler作法不一。 • 但解答許多問題必須根源與我們對C++ object model的瞭解。

  4. Class • Class是一種abstract data type. • 在class宣告時,包含兩種成員: • 1.data member :描述class object之屬性。 • 2.member function:描述class object之行為。 • 利用class這樣的abstract data type,我們可以定義出(造出)許多的這種 type 的instantiation叫做object。

  5. Static Data Member • 屬於class層級的資料,所有object共享一份static data member。 • 存取static member不需要透過任何的object,在無任何object時已透過member selection operators 來存取。 • 所有的存取都會被compiler轉化為extern實體的直接參考動作 Ex. CPoint3D::size = 300;

  6. Nonstatic Data Member • 對於nonstatic data member的存取,實際上是透過implicit的this指標來完成。 • 存取會轉換成 &(this) + (data member offset) • 由於offset在compiler time就可算出,其效率等於存取一個C struct member.

  7. Single Object layout • 一個object的實體之內只含class 之 nonstatic data member及某些輔助機制(vptr)。 • Class member functions 獨立於object實體之外是獨一無二的一份函示實體(即多個object共享相同的函示實體。) • 同一個access section的data members排列順序根據其宣告順序而定。 • Access section的排列順序無強制規定。

  8. Single Object layout(cont. 2) • Class CPoint3d{ public: //….. Private: float x; static int size = 250; int y; void draw(); static int point- count(); private: char z; }; CPoint3d object x y z

  9. Inheritance • 繼承之於Nonstatic data members,是指base object members存在在derived object 中。 • 繼承之於Member functions,是指繼承了對base class’s member function的呼叫權利。 • 繼承之於Static data member,是指繼承了對bass class static data member的存取權利。

  10. 物件模型在繼承下Layout的原則 • C++保證,『出現在derived class中的bass class subobject有其完整之原樣性』。 • Derived class layout = [ direct bass class ]s + [ 自己新增的data member (nonstatic data or vptr) ]

  11. float x; float x; float y; float y; float z; oPt2d oPt3d 單一繼承的物件模型 • 單一繼承:指每一個class的direct bass class只能有一個,繼承的深度沒有限制。 Class CPoint2d{ public : ….. protected : float x; float y; } oPt2d ; Class CPoint3d : public CPoint2d { public: … protected: float z; }oPt3d ;

  12. float x; float y; float z; * next; 多重繼承的物件模型 • 多重繼承:指每一各class有兩個以上的direct base class,繼承的深度沒有限制。 Class CPoint3dV: public CPoint2d ,public CVertex { public: … protected: float z; }oPt3dV ; Class CVertex{ Vertex *next; } oV; * next; oV oPt3dV

  13. 虛擬繼承 • Shared subobject 繼承的機制。 • 多重繼承與虛擬多重繼承的比較: ios ios ios ostream ostream istream istream iostream iostream 多重繼承 虛擬多重繼承

  14. Class Point2d{ public : … protected: float x; float y; }; Class Point3d: public virtual Point2d{ public : … protected : float z; }; Class Vertex: public virtual Point2d{ public : … protected : vertex *next; }; Class Vertex3d: public virtual Vertex, public Point3d { public : … protected : float z; }; 虛擬繼承的物件模型(1) • 下面是Cvertex3d虛擬繼承的架構:

  15. Vertex *next Vertex *next vpbassPoint2d vpbassPoint2d Vptr_Point2d Vptr_Point2d Vptr_Point2d Vptr_Point2d vptr_Vertex vptr_Vertex float x float x float x float x float y float y float y float y Point3d oPt3d Point oPt2d float z float z vpbassPoint2d vpbassPoint2d vptr_point3d vptr_point3d Float mumble Vertex3d oV3d Vertex oV 虛擬繼承的物件模型(2)

  16. Polymorphism(1) • 多型:以同一指標指向不同型別的物件。 • 群:一群“同型”但不全然相同的個體。Ex.一群大學生。 • 多型抽象目的:利用“群”的觀念,實現general演算法,並保有“個體”間的差異。 • 不同的derived class object彼此之間有差異,但屬於相同的bass class“群” 。 • 多型實做方式:bass point代表群,供general演算法來使用,bass point可指向向其derived class object保有個體差異。

  17. Polymorphism(2) • Ex. Bass class 魚, drive class 大肚魚,金魚,鯊魚 魚的point可以指向大肚魚、金魚、鯊魚魚的object。利用演算法:『由第一隻魚到最後一隻魚,魚開始游泳』可使所有的魚都開始游泳,但不同類的魚會依照自己的方式來游。 • 演算法簡潔,且保持個體之間的特性。 • 演算法設計者不需瞭解物件的如何實做。

  18. C++如何支援多型 • 經由隱含轉型動作,允許derived class object轉化給base type point來指。 • 經由虛擬機制喚起指標所指之object的虛擬函式實體。 • Object保有type-info。 • Dynamic_cast可對Object point做型別轉換的安全檢查。

  19. Vertex *next vpbassPoint2d vptr_Vertex float z vpbassPoint2d vptr_point3d Unnatural polymorphism • 當derive objectassign 給bass pointer時,需暗含this point位移調整時,稱unnatural polymorphism。 • 暗含的位移調整工作由complier偷偷插入程式之中。 • Ex. Vertext *pVertex = new Vertext3d; this pointer (offset) *pVertex Vertex3d Vertex * object

  20. Virtual function • Ex. Point2d * pPoint = new Vertex3d; pPoint->draw(); • 當你希望draw()的呼叫是根據pPoint所指的物件而不是pPoint的型別時,draw()必須是virtual function。 • Virtual function :使當利用指標來呼叫函式時,呼叫的函式實體是根據指標所指的物件的型別來決定,而不是根據指標的型別來決定。

  21. Virtual function in Object model • 每個object內有一個vptr指標,指向virtual function table。 • 每一個class有一個virtual function table(供object之vptr所指)內含class之中有作用的virtual function的address。 • Virtual function table 的index 0 存放的是type-info用以支援runtime type identification (RTTI)。

  22. float x; Type_info * vptr ; ~point() Virtual table for point show() point(float) point-count PointCount() x() C++物件模型 • 兩種data member : static/nonstatic data; • 三種memberfunction: static/nonstatic/virtual function; Class Point{ public: point(float xtal); virtual ~Point(); float x() const; static int PointCount(); protected : virtual void show(); float x; static int point-count; };

More Related