Preview What is C object model Class and


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


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

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

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


Single Object layout (cont. 2) • Class CPoint 3 d{ public: //…. . Private: float x; static int size = 250; int y; void draw(); static int pointcount(); private: char z; }; CPoint 3 d object x y z

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

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

單一繼承的物件模型 • 單一繼承:指每一個class的direct bass class只能有一個,繼承的深度沒有限制。 Class CPoint 2 d{ public : …. . protected : float x; float y; } o. Pt 2 d ; Class CPoint 3 d : float x; float y; o. Pt 2 d public CPoint 2 d { public: … protected: float z; }o. Pt 3 d ; float x; float y; float z; o. Pt 3 d

多重繼承的物件模型 • 多重繼承:指每一各class有兩個以上的 direct base class,繼承的深度沒有限制。 Class CPoint 3 d. V: Class CVertex{ Vertex *next; } o. V; * next; o. V public CPoint 2 d , public CVertex { public: … protected: float z; }o. Pt 3 d. V ; float x; float y; * next; float z; o. Pt 3 d. V


虛擬繼承的物件模型(1) • 下面是Cvertex 3 d虛擬繼承的架構: Class Point 2 d{ public : … protected: float x; float y; }; Class Vertex: public virtual Point 2 d{ public : … protected : vertex *next; }; Class Point 3 d: public virtual Point 2 d{ public : … protected : float z; }; Class Vertex 3 d: public virtual Vertex, public Point 3 d { public : … protected : float z; };

虛擬繼承的物件模型(2) Vptr_Point 2 d float x float y Point o. Pt 2 d Vertex *next vpbass. Point 2 d vptr_Vertex Vptr_Point 2 d float x float y Vertex o. V float z vpbass. Point 2 d vptr_point 3 d Vptr_Point 2 d float x float y Point 3 d o. Pt 3 d Vertex *next vpbass. Point 2 d vptr_Vertex float z vpbass. Point 2 d vptr_point 3 d Float mumble Vptr_Point 2 d float x float y Vertex 3 d o. V 3 d




Unnatural polymorphism • 當derive object assign 給bass pointer時, 需暗含this point位移調整時,稱unnatural polymorphism。 • 暗含的位移調整 作由complier偷偷插入 程式之中。 • Ex. Vertext *p. Vertex = new Vertext 3 d; this pointer *p. Vertex * (offset) Vertex *next vpbass. Point 2 d vptr_Vertex float z vpbass. Point 2 d vptr_point 3 d Vertex 3 d object


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)。

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