• <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>

            Benjamin

            靜以修身,儉以養德,非澹薄無以明志,非寧靜無以致遠。
            隨筆 - 397, 文章 - 0, 評論 - 196, 引用 - 0
            數據加載中……

            C++之virtual functions(虛函數)實現細節及相關概念

            c++中的(static) type和 (dynamic) type 概念是基于多態(polymorphism) ,例如:Vehicle*指針如果實際是指向一個Car對象,那么這個指針的靜態類型就是Vechicle,Car則是他的動態類型。靜態類型發生在編譯器編譯時,動態類型發生在動態綁定時。
            我們常說的override(覆蓋)就是針對虛函數而言。
            對虛函數的實現應該說各個編譯器是不一樣的,大多數的編譯器是這樣的:
            為每一個有虛函數的類增加一個虛表,這個虛表是靜態的,還有一個虛指針,為每個類對象。例如:
            // Your original C++ source code
             class Base {
             public:
               virtual arbitrary_return_type virt0(...arbitrary params...);
               virtual arbitrary_return_type virt1(...arbitrary params...);
               virtual arbitrary_return_type virt2(...arbitrary params...);
               virtual arbitrary_return_type virt3(...arbitrary params...);
               virtual arbitrary_return_type virt4(...arbitrary params...);
               ...
             };
            1 編譯器會為這個類的虛函數添加一個虛表,類似下面的:
            // Pseudo-code (not C++, not C) for a static table defined within file Base.cpp
             
             // Pretend FunctionPtr is a generic pointer to a generic member function
             // (Remember: this is pseudo-code, not C++ code)
             FunctionPtr Base::__vtable[5] = {
               &Base::virt0, &Base::virt1, &Base::virt2, &Base::virt3, &Base::virt4
             };

            2 然后增加一個指向虛表的指針為每一個類對象,這個指針是隱藏的
             // Your original C++ source code
             
             class Base {
             public:
               ...
               FunctionPtr* __vptr;  ← supplied by the compiler, hidden from the programmer
               ...
             };
            3 編譯器在構造中初始化這個指針
            Base::Base(...arbitrary params...)
               : __vptr(&Base::__vtable[0])  ← supplied by the compiler, hidden from the programmer
               ...
             {
               ...
             }
            在派生類中,它也會增加一個隱藏的虛表,但是它可以overrides基類的虛函數如:
            // Pseudo-code (not C++, not C) for a static table defined within file Der.cpp
             
             // Pretend FunctionPtr is a generic pointer to a generic member function
             // (Remember: this is pseudo-code, not C++ code)
             FunctionPtr Der::__vtable[5] = {
               &Der::virt0, &Der::virt1, &Der::virt2, &Base::virt3, &Base::virt4
             };    

            最后看看底層是如何調用的如:                                  
            void mycode(Base* p)
             {
               p->virt3();
             }
            主要三部分:
            1.獲取隱藏的指向虛表的指針,并把它放在 register中如r1;
            2.獲取指針r2=r1+3*4(假定一個指針有四個字節) ,并把它放到register中。
            3 根據r2的地址調用函數。

            所以說,調用一個虛函數至少和非虛函數差不多.
            在這里我們可以看出一個虛指針的長度,至少是四個字節,但是要注意編譯器對它的具體實現

            純虛函數怎樣用,下面的例子可以說明這個問題。
            象下面的代碼就可以用純虛函數來實現:
            typedef std::vector<Vehicle*>  VehicleList;
             
             void myCode(VehicleList& v)
             {
               for (VehicleList::iterator p = v.begin(); p != v.end(); ++p) {
                 Vehicle& v = **p;  // just for shorthand
             
                 // generic code that works for any vehicle...
                 ...
             
                 // perform the "foo-bar" operation.
                 // note: the details of the "foo-bar" operation depend
                 // on whether we're working with a car or a truck.
                 if (v is a Car) {
                   // car-specific code that does "foo-bar" on car v
                   ...
                 } else if (v is a Truck) {
                   // truck-specific code that does "foo-bar" on truck v
                   ...
                 } else {
                   // semi-generic code that does "foo-bar" on something else
                   ...
                 }
             
                 // generic code that works for any vehicle...
                 ...
               }
             }
            用純虛函數實現如下:
            class Vehicle {
             public:
               // performs the "foo-bar" operation
               virtual void fooBar() = 0;
             };
            typedef std::vector<Vehicle*>  VehicleList;
             
             void myCode(VehicleList& v)
             {
               for (VehicleList::iterator p = v.begin(); p != v.end(); ++p) {
                 Vehicle& v = **p;  // just for shorthand
             
                 // generic code that works for any vehicle...
                 ...
             
                 // perform the "foo-bar" operation.
                 v.fooBar();
             
                 // generic code that works for any vehicle...
                 ...
               }
             }

            也可以用繼承的方法來實現
            class Car : public Vehicle {
             public:
               virtual void fooBar();
             };
             
             void Car::fooBar()
             {
               // car-specific code that does "foo-bar" on 'this'
               ...  ← this is the code that was in {...} of if (v is a Car)
             }
             
             class Truck : public Vehicle {
             public:
               virtual void fooBar();
             };
             
             void Truck::fooBar()
             {
               // truck-specific code that does "foo-bar" on 'this'
               ...  ← this is the code that was in {...} of if (v is a Truck)
             }
            有純虛函數的是抽象基類,強迫派生類接受基類的接口并實現,在COM(組件)中比較常見,。

            virtual constructor(虛構造)的一個實現方法之一:
            class Shape {
             public:
               virtual ~Shape() { }                 // A virtual destructor
               virtual void draw() = 0;             // A pure virtual function
               virtual void move() = 0;
               ...
               virtual Shape* clone()  const = 0;   // Uses the copy constructor
               virtual Shape* create() const = 0;   // Uses the default constructor
             };
             
             class Circle : public Shape {
             public:
               Circle* clone()  const;   // Covariant Return Types; see below
               Circle* create() const;   // Covariant Return Types; see below
               ...
             };
             
             Circle* Circle::clone()  const { return new Circle(*this); }
             Circle* Circle::create() const { return new Circle();      }

            void userCode(Shape& s)
             {
               Shape* s2 = s.clone();
               Shape* s3 = s.create();
               ...
               delete s2;    // You need a virtual destructor here
               delete s3;
             }
            注意在VC6中必須寫成Shape*,VC7就不用改,支持返回類型可以變。

            posted on 2009-06-13 17:38 Benjamin 閱讀(2477) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            久久精品免费网站网| 99久久精品国产一区二区| 日韩精品久久久久久| 久久久久国产亚洲AV麻豆| 亚洲精品国产字幕久久不卡| 热久久这里只有精品| 久久精品国产亚洲AV蜜臀色欲| 2021精品国产综合久久| 蜜臀久久99精品久久久久久| 奇米影视7777久久精品| 久久综合狠狠综合久久97色| 久久人人爽人人爽人人AV东京热| 久久久久国色AV免费看图片| 潮喷大喷水系列无码久久精品| 色婷婷久久综合中文久久一本| 久久精品中文騷妇女内射| 久久99这里只有精品国产| 国产免费福利体检区久久 | 国产精品美女久久久免费| 99久久99久久精品国产片果冻| 久久国产成人午夜AV影院| 亚洲一区中文字幕久久| 国产亚洲婷婷香蕉久久精品| 一本久道久久综合狠狠爱| 亚洲国产成人久久一区WWW| 精品一区二区久久| 久久精品国产亚洲AV麻豆网站| 亚洲午夜福利精品久久| 久久久久亚洲爆乳少妇无| 久久久久亚洲AV无码专区网站| 国产精品美女久久久久av爽| 国产精品成人久久久久三级午夜电影 | 色综合久久无码中文字幕| 香蕉aa三级久久毛片| 久久午夜福利电影| 亚洲婷婷国产精品电影人久久| 久久九九久精品国产免费直播| 国产综合精品久久亚洲| 久久夜色精品国产| 三级三级久久三级久久| 久久精品国产久精国产果冻传媒|