• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            高山流水

            ----- 要黑就黑徹底

            Inside The C++ Object Model 學習筆記--Semantics of Construction, Destructin, and Copy

            Chapter 5. Semantics of Construction, Destruction, and Copy

            5.1 “無繼承” 情況下的對象構造
            1. 普通類型(和C相同)
            2. 抽相數據類型
            3. 為繼承作準備

            5.2 繼承體系下對象的構造
            1.? 通用繼承構造規則
            ???? (1) 在成員初始化列表中的data members初始化操作會被放進constructor的函數本身,并以members的聲明順序為順序。
            ???? (2) 如果有一個member沒有出現在初始化列表中,但它有一個default constructor,那么default constructor會被調用。
            ???? (3) 如果class object 有virtual table pointer(s), 它(們)必須被設定初始值,指向適當的virtual table(s)。
            ???? (4) 在那之前,所有的上一層的base class constructors必須被調用, 以base class的聲明順序為順序
            ???? (5) 在那之前,所有的virtual base class constructors必須被調用,從左到右,從最深到最淺。

            2. 虛擬繼承(virtual Inheritance)
            如對于下面的類:?

            ?1?class?Point3d?:?public?virtual?Point
            ?2??{?
            ?3??public:?
            ?4?????Point3d(?float?x?=?0.0,?float?y?=?0.0,?float?z?=?0.0?)?
            ?5?????????:?Point(?x,?y?),?_z(?z?)?{}?
            ?6?????Point3d(?const?Point3d&?rhs?)?
            ?7?????????:?Point(?rhs?),?_z(?rhs._z?)?{}?
            ?8?????~Point3d();?
            ?9?????Point3d&?operator=(?const?Point3d&?);?
            10?
            11?????virtual?float?z(){?return?_z;?}?
            12?????//??
            13??protected:?
            14?????float?_z;?
            15??};?

            ?
            ?可能的轉換是這樣的:

            ?1?//?Psuedo?C++?Code:?
            ?2??//?Constructor?Augmentation?with?Virtual?Base?class?
            ?3??Point3d*?Point3d::Point3d(?Point3d?*this,?bool?__most_derived,??float?x,?float?y,?float?z?)?
            ?4??{?
            ?5???if?(?__most_derived?!=?false?)?
            ?6????this->Point::Point(?x,?y);?
            ?7?
            ?8???this->__vptr__Point3d?=?__vtbl__Point3d;?
            ?9???this->__vptr__Point3d__Point?=?__vtbl__Point3d__Point;?
            10?
            11???this->_z?=?rhs._z;?
            12???return?this;?
            13??}?

            ?//
            ?對于如下的繼承層次:
            ?class Vertex?? : virtual public Point { ... };
            ?class Vertex3d : public Point3d, public Vertex { ... };
            ?class PVertex? : public Vertex3d { ... };

            ?類Point3d的構造可能是:

            ?1?//?Psuedo?C++?Code:?
            ?2??//?Constructor?Augmentation?with?Virtual?Base?class?
            ?3??Point3d*?Point3d::Point3d(?Point3d?*this,?bool?__most_derived,??float?x,?float?y,?float?z?)?
            ?4??{?
            ?5???if?(?__most_derived?!=?false?)?
            ?6????this->Point::Point(?x,?y);?
            ?7?
            ?8???this->__vptr__Point3d?=?__vtbl__Point3d;?
            ?9???this->__vptr__Point3d__Point?=?__vtbl__Point3d__Point;?
            10?
            11????this->_z?=?rhs._z;?
            12???return?this;?
            13??}

            ?//
            ?對于Vertex3d的構造可能是如下:

            ?1?//?Psuedo?C++?Code:?
            ?2??//?Constructor?Augmentation?with?Virtual?Base?class?
            ?3??Vertex3d*?Vertex3d::Vertex3d(?Vertex3d?*this,?bool?__most_derived,?float?x,?float?y,?float?z?)?
            ?4??{?
            ?5???if?(?__most_derived?!=?false?)?
            ?6????this->Point::Point(?x,?y);?
            ?7?
            ?8???//?invoke?immediate?base?classes,?
            ?9???//?setting?__most_derived?to?false?
            10?
            11???this->Point3d::Point3d(?false,?x,?y,?z?);?
            12???this->Vertex::Vertex(?false,?x,?y?);?
            13?
            14???//?set?vptrs?
            15???//?insert?user?code?
            16?
            17???return?this;?
            18??}?

            ?

            3. vptr初始化語意學(The Semantics of the vptr Initialization)
            ???? (1) 構造函數執行算法
            ????????? I.??? 在derived class constructor 中, 所有的"virtual base classes" 及 "上一層base class"的constructors會被調用.
            ?? II.? 上述完成之后, 對象的vptr(s)會被初始化, 指向相關的virtual talbe(s).
            ?? III. 如果有成員初始化列表的話, 將在constructor體內擴展開來. 這必須在vptr被設定之后才能進行,以免有一個virtual member function被調用
            ?? IV. 最后, 執行程序所提供的代碼.
            ????
            ???? (2) 示例:
            ???? For example, given the following user-defined PVertex constructor:

            1?PVertex::PVertex(?float?x,?float?y,?float?z?)?
            2?????:?_next(?0?),?Vertex3d(?x,?y,?z?)
            3?????,?Point(?x,?y?)?
            4??{?
            5???if?(?spyOn?)?
            6????cerr?<<?"within?Point3d::Point3d()"??<<?"?size:?"?<<?size()?<<?endl;?
            7??}?

            ?

            ??可能一個擴展如下:

            ?1?//?Pseudo?C++?Code?
            ?2??//?expansion?of?PVertex?constructor?
            ?3??PVertex*?PVertex::PVertex(?Pvertex*?this,??bool?__most_derived,?float?x,?float?y,?float?z?)?
            ?4??{?
            ?5???//?conditionally?invoke?the?virtual?base?constructor?
            ?6???if?(?__most_derived?!=?false?)?
            ?7????this->Point::Point(?x,?y?);?
            ?8???//?unconditional?invocation?of?immediate?base?
            ?9????this->Vertex3d::Vertex3d(?x,?y,?z?);?
            10?
            11???//?initialize?associated?vptrs?
            12?
            13???this->__vptr__PVertex?=?__vtbl__PVertex;?
            14???this->__vptr__Point__PVertex?=?__vtbl__Point__PVertex;?
            15?
            16???//?explicit?user?code?
            17???if?(?spyOn?)?
            18????cerr?<<?"within?Point3d::Point3d()"?
            19????<<?"?size:?"?<<?(*this->__vptr__PVertex[?3?].faddr)(this)?
            20????<<?endl;?
            21?
            22???//?return?constructed?object?
            23???return?this;?
            24??}?

            ?


            5.3 對象的復制(Object Copy Sematics)
            1. Copy constructor operator 在以下幾種情況下不會表現出: bitwisecopy
            ???? (1) class 內帶有一個member object, 而其類有一個copy constructor operator時。
            ???? (2)? 當一個 class 的base 有一個copy assignment operator時。
            ???? (3)? 當類聲明了任何一個virtual functions時。
            ???? (4)? 當class繼承自一個virtual base class時
            ?2. 合成示例

            ?1??//?Pseudo?C++?Code:?synthesized?copy?assignment?operator?
            ?2??inline?Point3d&?Point3d::operator=(?Point3d?*const?this,?const?Point3d?&p?)?
            ?3??{?
            ?4???//?invoke?the?base?class?instance?
            ?5???this->Point::operator=(?p?);?
            ?6?
            ?7???//?memberwise?copy?the?derived?class?members?
            ?8???_z?=?p._z;?
            ?9???return?*this;?
            10??}?

            ?

            5.4 對象的功能


            5.5 析構語意學(Semantics of Destruction)
            1. 析構函數生成原則:
            ?如果類沒有定義destructor, 那么只有在class內帶的member object(或是class自己的base class)擁有destructor的情況下,編譯器才會自動的合成一個來。否則,destructor會被視為不需要,也就不需要被合成(當然更不需要被調用)

            2. 析構調用過程
            ???? (1) destructor的函數本身首先被執行。
            ???? (2) 如果class擁有member class objects, 而后者擁有destructors, 那么它會以其聲明順序的相反順序被調用.
            ???? (3) 如果object內帶一個vptr, 則現在被重新設定,指向適當的base class的virtual table.
            ???? (4) 如果任何直接的(上一層)novirtual base classes 擁有destructor,那么它會以其聲明順序相反的順序調用
            ???? (5) 如果有任何的virtual base classes 擁有destructor, 而當前討論的這個class是最末端的class, 那么它們會以其原來的構造順序相反的順序被調用.

            ?

            posted on 2006-10-30 14:42 猩猩 閱讀(240) 評論(0)  編輯 收藏 引用 所屬分類: C&C++語言

            精品久久777| 欧美亚洲日本久久精品| 无码精品久久久久久人妻中字| 久久久久亚洲AV无码观看| 久久精品免费一区二区| 久久久久亚洲av无码专区| 久久不射电影网| 久久亚洲日韩看片无码| 日本久久久精品中文字幕| 性欧美大战久久久久久久| 国产欧美久久久精品| 亚洲国产成人精品久久久国产成人一区二区三区综 | 无码精品久久久久久人妻中字| 国产午夜福利精品久久2021| 欧美大战日韩91综合一区婷婷久久青草 | 国产精品久久久久影院嫩草 | 精品久久久久久久久午夜福利| 99久久国产主播综合精品 | 99久久免费国产精品| 亚洲国产精品无码久久SM| 久久亚洲中文字幕精品一区| av无码久久久久不卡免费网站| 久久久久国产精品人妻| 久久久综合香蕉尹人综合网| 国产精品久久亚洲不卡动漫| 久久久久久久97| 久久久久久曰本AV免费免费| 亚洲精品视频久久久| 日韩中文久久| 久久久久18| 国产成人99久久亚洲综合精品| 欧美va久久久噜噜噜久久| 亚洲午夜久久久久久久久电影网| 久久久久亚洲AV成人网人人软件| 88久久精品无码一区二区毛片| 久久免费高清视频| 精品999久久久久久中文字幕| 精品国产乱码久久久久久郑州公司 | 亚洲精品国产字幕久久不卡| 久久久久久久波多野结衣高潮| 伊人久久大香线蕉综合Av |