• <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 閱讀(230) 評論(0)  編輯 收藏 引用 所屬分類: Questions

            欧美精品福利视频一区二区三区久久久精品 | 精品久久香蕉国产线看观看亚洲| 一个色综合久久| 久久久久黑人强伦姧人妻| 26uuu久久五月天| 国产精品青草久久久久福利99 | 久久久WWW成人免费精品| 久久99精品国产麻豆不卡| 日韩一区二区久久久久久| 久久久久国产视频电影| 久久人搡人人玩人妻精品首页 | 女人香蕉久久**毛片精品| 中文字幕成人精品久久不卡| 天天久久狠狠色综合| 久久av高潮av无码av喷吹| 日韩中文久久| 中文国产成人精品久久不卡| 久久精品人成免费| 亚洲综合婷婷久久| 人人狠狠综合88综合久久| 精品一二三区久久aaa片| 国产精品一区二区久久| 久久久久人妻一区精品| 久久伊人五月丁香狠狠色| 久久超碰97人人做人人爱| 999久久久国产精品| 一级女性全黄久久生活片免费 | 亚洲午夜精品久久久久久浪潮| 99精品国产99久久久久久97| 久久精品免费一区二区三区| 欧美激情精品久久久久久久| 97久久精品无码一区二区天美| 国产成人精品久久综合 | 久久精品毛片免费观看| 欧美午夜A∨大片久久| 欧美牲交A欧牲交aⅴ久久| 久久最新免费视频| 久久精品视频免费| 亚洲乱码中文字幕久久孕妇黑人| 精品久久久久久99人妻| 精品久久一区二区|