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

            国产激情久久久久影院老熟女| 久久99国产精品久久久| 日本亚洲色大成网站WWW久久| 午夜精品久久久久久| 无码超乳爆乳中文字幕久久| 久久久青草久久久青草| 日韩中文久久| 久久99精品久久久久久动态图| 久久久久久免费一区二区三区| 亚洲国产成人精品91久久久 | 亚洲中文字幕无码久久精品1| 国内精品久久久久影院优| 久久久久成人精品无码| 久久久亚洲欧洲日产国码二区| 久久久久成人精品无码| 国产午夜久久影院| 久久99这里只有精品国产| 狠狠干狠狠久久| 久久精品国产亚洲精品2020 | 久久国产乱子伦免费精品| 无夜精品久久久久久| 久久av免费天堂小草播放| 丁香狠狠色婷婷久久综合| 亚洲中文字幕久久精品无码喷水| 四虎影视久久久免费观看| 国产成人精品综合久久久| 久久国产精品久久国产精品| 精品乱码久久久久久久| 漂亮人妻被黑人久久精品| 国内精品伊人久久久久777| 亚洲国产日韩欧美综合久久| 日韩中文久久| 色播久久人人爽人人爽人人片AV| 久久伊人五月天论坛| 久久亚洲高清综合| 欧美精品一区二区久久| 国产精品内射久久久久欢欢| 久久www免费人成精品香蕉| 久久久久久噜噜精品免费直播 | 1000部精品久久久久久久久| 囯产精品久久久久久久久蜜桃|