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

            久久久久亚洲?V成人无码| 国产免费福利体检区久久| 久久久久国产精品嫩草影院| 成人a毛片久久免费播放| 久久久久婷婷| 日韩精品久久久久久久电影蜜臀| 性做久久久久久久久久久| 亚洲va久久久噜噜噜久久男同| 久久精品成人免费看| 国内精品人妻无码久久久影院导航| 久久久91精品国产一区二区三区| 久久无码专区国产精品发布| 国产叼嘿久久精品久久| 9999国产精品欧美久久久久久| 欧美黑人激情性久久| 亚洲伊人久久精品影院| 亚洲精品无码成人片久久| 色婷婷噜噜久久国产精品12p| 996久久国产精品线观看| 国产午夜精品久久久久九九电影| 欧美亚洲国产精品久久蜜芽| 伊人久久大香线焦综合四虎| 99久久综合国产精品二区| 亚洲乱亚洲乱淫久久| 久久久久久亚洲精品影院| 久久这里只有精品首页| 久久A级毛片免费观看| 四虎影视久久久免费观看| 偷偷做久久久久网站| 精品久久久久久中文字幕| 国产高清美女一级a毛片久久w| 亚洲国产精品嫩草影院久久| 久久夜色精品国产噜噜噜亚洲AV| 国产精品久久久久久福利漫画 | 久久综合88熟人妻| 久久久久久夜精品精品免费啦 | 青青热久久国产久精品| 久久久久久国产精品免费免费| 狠狠久久亚洲欧美专区| 天天综合久久久网| 色综合久久最新中文字幕|