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

            EverSpring working shop

            To pursue creative ideas based on nature.

            統計

            留言簿(1)

            他山之石

            閱讀排行榜

            評論排行榜

            RTTI and Down-Casting

            RTTI used in the down-casting of the base type to derived type:
            ?
            Question:
            Why is it dangerous that the cast a base type pointer/reference to the derived pointer/reference? And how to ensure a safe casting in our application programming.
            Answer:
            If a pointer or reference is actually representing a base type object, it is really dangerous to do down-casting from the base type to derived type, because the derived may have the memners which do not exist in the base type. The incorrect memory writing/reading will occur under such a situation.
            We can use the RTTI to ensure the safe casting, implemented by the "dynamic_cast<derived_type*>".
            (Note: RTTI should be applied by the compiler)
            ?
            Codes example:
            ?
            class base_type
            {
            public:
            ??? int a;
            ??? void set_a(int aa){a = aa;}
            };
            ?
            class derived_type:public base_type
            {
            public:
            ??? int b
            ????void set_b(int bb){b = bb;}
            }
            ?
            void fun(base_type* p_base)
            {
            ??? int _a=1, _b=2;
            ?
            ??? derived_type* p_derived = (derived_type*)p_base;? // DANGEROUS!!
            ??? p_derived->set_b(_b);? // Error, If the p_base points to a pure base_type object
            ?
            ??? // Safe casting:
            ????derived_type* p2_derived;
            ??? if (p2_dervided = dynamic_cast<derived_type*>p_base)?
            ??? {
            ??????? //If p_base points to a derived_type object, p2_derived != NULL
            ??????? p2_derived->set_b(_b); //No problem, because *p2_dervied is a real dervied object
            ??? }
            ??? else
            ??? {
            ??????? cout<<"\n a non-derived class obj reference passed in.";
            ??? }
            }
            ?
            Alex Zhang
            ?

            posted on 2008-01-19 23:57 everspring79 閱讀(217) 評論(0)  編輯 收藏 引用 所屬分類: Questions

            久久国产免费直播| 天堂无码久久综合东京热| 亚洲国产成人久久精品99| 伊人久久大香线蕉综合网站| 日本国产精品久久| 亚洲第一极品精品无码久久 | 久久亚洲AV成人无码软件| 久久人人爽人人爽人人片av高请| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 国产精品青草久久久久福利99| 久久久久久久久久久久中文字幕| 91精品国产高清久久久久久91 | 亚洲αv久久久噜噜噜噜噜| 国产国产成人精品久久| 国产欧美久久久精品影院| 久久人妻少妇嫩草AV无码专区| 久久久精品国产Sm最大网站| 久久91精品国产91久久户| 久久精品水蜜桃av综合天堂| 久久精品国产一区二区三区| 久久精品国产亚洲精品2020| 无夜精品久久久久久| 国产成人无码精品久久久免费| 久久久久久亚洲Av无码精品专口| 亚洲性久久久影院| 久久久久久久综合日本| 7国产欧美日韩综合天堂中文久久久久 | 97视频久久久| 国内精品久久久久影院一蜜桃 | 久久天天婷婷五月俺也去| 香蕉久久影院| 久久久精品国产Sm最大网站| 久久国产亚洲精品无码| 久久人人爽人人爽人人片AV东京热| 91精品国产91久久久久久蜜臀 | 久久精品无码av| 狠狠精品久久久无码中文字幕 | 久久精品午夜一区二区福利| 精品人妻伦九区久久AAA片69| 国产精品久久婷婷六月丁香| 亚洲AⅤ优女AV综合久久久|