• <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>
            asm, c, c++ are my all
            -- Core In Computer
            posts - 139,  comments - 123,  trackbacks - 0
            [轉]Run time type Information

            一???????????????? dynamic_cast
            < type-id > ( expression )
            ? type-id :必須是指針或者引用
            ? expression:必須是指針或者一個左值
            基本上是用來做 子對象間的轉換
            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.
            小結:
            ? a???? 實際指向對象 和?? b 目標對象,? b 是a 的 基類(子對象),或者是2者 同級轉換才成功
            否則 目標為NULL;也就是說不能向下轉



            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意性下會失敗
            3.子對象間的轉換

            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 )?
            ?? 此操作符返回個? const type_info &?
            ??????????? expression?必須指向一個多態類型(帶虛函數??? ),并且要解引用

            #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 );
            }

            -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

            還不知道dynamic_cast<void*>()能轉換成真實類型的指針。有這個功能,真太好了。
            等C++0x出來后,就可以用:
            auto pv = dynamic_cast<void*>(...)來獲取真實類型了。

            posted on 2006-07-04 21:03 Jerry Cat 閱讀(310) 評論(0)  編輯 收藏 引用

            <2006年7月>
            2526272829301
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            常用鏈接

            留言簿(7)

            隨筆檔案

            最新隨筆

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久综合九色综合网站| 国产成人香蕉久久久久| 天天躁日日躁狠狠久久| 久久婷婷五月综合色奶水99啪| 国产精品久久久久久一区二区三区| 亚洲精品高清久久| 久久亚洲欧洲国产综合| 久久精品中文闷骚内射| 久久久久久无码国产精品中文字幕| 亚洲va久久久噜噜噜久久男同| 久久精品免费一区二区三区| 亚洲欧美成人久久综合中文网| 欧美黑人又粗又大久久久| 四虎国产精品免费久久| 国产成人综合久久综合| 香蕉久久夜色精品国产尤物| 成人久久久观看免费毛片| 久久人做人爽一区二区三区 | 伊人久久大香线蕉av一区| 18岁日韩内射颜射午夜久久成人| 亚洲中文久久精品无码ww16| 精品综合久久久久久88小说 | 日本亚洲色大成网站WWW久久 | 亚洲精品NV久久久久久久久久| 97久久香蕉国产线看观看| 亚洲va久久久噜噜噜久久男同| 日本精品久久久久影院日本| 久久无码精品一区二区三区| 久久国产成人午夜aⅴ影院| 女人香蕉久久**毛片精品| 久久久久亚洲av无码专区| 日韩精品久久无码人妻中文字幕| 久久久国产视频| 久久WWW免费人成一看片| 久久久久久久波多野结衣高潮| 亚洲国产精品狼友中文久久久| 久久久久无码精品| 久久天天婷婷五月俺也去| 久久久久久久女国产乱让韩| 99久久99久久精品国产片果冻 | 精品人妻伦一二三区久久 |