• <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>
            隨筆 - 25  文章 - 29  trackbacks - 0
            <2006年7月>
            2526272829301
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            常用鏈接

            留言簿(4)

            隨筆分類(22)

            隨筆檔案(25)

            文章分類(2)

            文章檔案(2)

            相冊

            最新隨筆

            搜索

            •  

            積分與排名

            • 積分 - 56387
            • 排名 - 404

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜


            一???????????????? dynamic_cast
            < type-id > ( expression )
            ? type-id :必須是指針或者引用
            ? expression:必須是指針或者一個(gè)左值
            基本上是用來做 子對象間的轉(zhuǎn)換
            1.基本的用法
            class B { ... };
            class C : public B { ... };
            class D : public C { ... };
            
            void f(D* pd)
            {
               C* pc = dynamic_cast<C*>(pd);   // ok: C is a direct base class
                                             // pc points to C subobject of pd 
            
               B* pb = dynamic_cast<B*>(pd);   // ok: B is an indirect base class
                                             // pb points to B subobject of pd 
               ...
            }

            This type of conversion is called an "upcast" because it moves a pointer up a class hierarchy, from a derived class to a class it is derived from. An upcast is an implicit conversion.

            If type-id is void*, a run-time check is made to determine the actual type of expression. The result is a pointer to the complete object pointed to by expression. For example:

            class A { ... };
            
            class B { ... };
            
            void f()
            {
               A* pa = new A;
               B* pb = new B;
               void* pv = dynamic_cast<void*>(pa);
               // pv now points to an object of type A
               ...
               pv = dynamic_cast<void*>(pb);
               // pv now points to an object of type B
            }

            If type-id is not void*, a run-time check is made to see if the object pointed to by expression can be converted to the type pointed to by type-id.

            If the type of expression is a base class of the type of type-id, a run-time check is made to see if expression actually points to a complete object of the type of type-id. If this is true, the result is a pointer to a complete object of the type of type-id. For example:

            class B { ... };
            class D : public B { ... };
            
            void f()
            {
               B* pb = new D;                     // unclear but ok
               B* pb2 = new B;
            
               D* pd = dynamic_cast<D*>(pb);      // ok: pb actually points to a D
               ...
               D* pd2 = dynamic_cast<D*>(pb2);   // pb2 points to a B not a D
                                                // cast was bad so pd2 == NULL
               ...
            }

            This type of conversion is called a "downcast" because it moves a pointer down a class hierarchy, from a given class to a class derived from it.
            小結(jié):
            ? a???? 實(shí)際指向?qū)ο?和?? b 目標(biāo)對象,? b 是a 的 基類(子對象),或者是2者 同級(jí)轉(zhuǎn)換才成功
            否則 目標(biāo)為NULL;也就是說不能向下轉(zhuǎn)



            2?? 多重非虛的繼承模式

            Class Hierarchy Showing Multiple Inheritance

            A pointer to an object of type D can be safely cast to B or C. However, if D is cast to point to an A object, which instance of A would result? This would result in an ambiguous casting error. To get around this problem, you can perform two unambiguous casts. For example:

            void f()
            {
               D* pd = new D;
               A* pa = dynamic_cast<A*>(pd);      // error: ambiguous
               B* pb = dynamic_cast<B*>(pd);      // first cast to B
               A* pa2 = dynamic_cast<A*>(pb);   // ok: unambiguous
            }

            Further ambiguities can be introduced when you use virtual base classes. Consider the class hierarchy shown in the following figure.

            ??? 也就是說在2意性下會(huì)失敗
            3.子對象間的轉(zhuǎn)換

            Class Hierarchy Showing Duplicate Base Classes


            ????//pd 指向 e 對象
            void f(D* pd)
            {
            ?? E* pe = dynamic_cast<E*>(pd);
            ?? B* pb = pe;????? // upcast, implicit conversion
            ?? A* pa = pb;????? // upcast, implicit conversion
            }

            //pd 指向 e 對象
            void f(D* pd)
            {
            ?? B* pb = dynamic_cast<B*>(pd);????? // cross cast
            ?? A* pa = pb;????????????????? // upcast, implicit conversion
            }

            ? 也就是? 所謂 cross cast

            二? typeid( type-id )???? typeid( expression )?
            ?? 此操作符返回個(gè)? const type_info &?
            ??????????? expression?必須指向一個(gè)多態(tài)類型(帶虛函數(shù)??? ),并且要解引用

            #include <iostream>
            #include <typeinfo.h>

            class Base {
            public:
            ?? virtual void vvfunc() {}
            };

            class Derived : public Base {};

            using namespace std;
            int main()
            {
            ?? Derived* pd = new Derived;
            ?? Base* pb = pd;
            ?? cout << typeid( pb ).name() << endl;?? //prints "class Base *"
            ?? cout << typeid( *pb ).name() << endl;?? //prints "class Derived"
            ?? cout << typeid( pd ).name() << endl;?? //prints "class Derived *"
            ?? cout << typeid( *pd ).name() << endl;?? //prints "class Derived"
            ?? delete pd;
            }
            // compile with: /GR /EHsc

            在模版中使用
            template < typename T > T max( T arg1, T arg2 ) {
            ?? cout << typeid( T ).name() << "s compared." << endl;
            ?? return ( arg1 > arg2 ? arg1 : arg2 );
            }


            ???????????

            posted on 2006-07-04 10:23 黃大仙 閱讀(1343) 評(píng)論(2)  編輯 收藏 引用 所屬分類: c++

            FeedBack:
            # re: Run time type Information 2006-07-04 14:53 沐楓
            好。
            還不知道dynamic_cast<void*>()能轉(zhuǎn)換成真實(shí)類型的指針。有這個(gè)功能,真太好了。
            等C++0x出來后,就可以用:
            auto pv = dynamic_cast<void*>(...)來獲取真實(shí)類型了。  回復(fù)  更多評(píng)論
              
            # re: Run time type Information 2006-07-08 00:27 flyingxu
            雖然是很好的文章,但是文章前面一部分和msnd上的太像了。。。可以多加些自己的理解  回復(fù)  更多評(píng)論
              
            久久久久久精品成人免费图片| 99久久亚洲综合精品成人| 中文字幕精品久久| 久久免费看黄a级毛片| 久久国产免费观看精品3| 久久久中文字幕| 国内精品久久国产| 狠狠色丁香婷婷综合久久来| 精品久久久久久久久久中文字幕 | 大美女久久久久久j久久| 久久国产综合精品五月天| 亚洲中文久久精品无码ww16| 国产精品亚洲综合专区片高清久久久| 久久精品中文字幕第23页| 久久精品欧美日韩精品| 精品人妻伦九区久久AAA片69| 久久精品免费全国观看国产| 久久夜色精品国产噜噜麻豆| 国产99久久久久久免费看| 无遮挡粉嫩小泬久久久久久久| 久久精品中文字幕一区| 77777亚洲午夜久久多喷| 欧美黑人又粗又大久久久| 久久婷婷色香五月综合激情| 久久99国产一区二区三区| 久久精品国产久精国产思思| 久久久久久国产精品美女| 亚洲精品视频久久久| 久久国产成人午夜aⅴ影院| 久久99精品国产| 久久综合丝袜日本网| 久久精品视频免费| 精品久久久久久无码专区| 欧洲成人午夜精品无码区久久| 亚洲国产成人久久综合区| 亚洲精品成人久久久| 婷婷国产天堂久久综合五月| 欧美亚洲另类久久综合婷婷| 久久这里只有精品视频99| 婷婷久久综合九色综合绿巨人 | 久久99精品久久久久久久久久|