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

            香港aa三级久久三级老师2021国产三级精品三级在 | 亚洲天堂久久精品| 日本久久久久亚洲中字幕| 久久大香香蕉国产| 亚洲中文久久精品无码ww16| 99久久精品国产麻豆| 日本精品久久久久中文字幕8 | 亚洲AV日韩精品久久久久久久| 国产精品免费福利久久| 99久久成人18免费网站| 久久精品国产亚洲AV影院| 国产激情久久久久影院小草 | 狠狠久久综合伊人不卡| 久久婷婷是五月综合色狠狠| 国产午夜免费高清久久影院 | 激情五月综合综合久久69| 久久天天婷婷五月俺也去| 麻豆久久| 久久免费线看线看| 国色天香久久久久久久小说| 久久综合伊人77777| 久久99国产精品尤物| 精品久久久久久中文字幕大豆网| 国产精品99久久久久久www| 久久久久久亚洲Av无码精品专口| 久久99九九国产免费看小说| 久久人人爽人人爽人人片AV东京热| 91久久九九无码成人网站| 波多野结衣久久精品| 久久99精品久久久久久9蜜桃| 色偷偷888欧美精品久久久| 国产精品青草久久久久婷婷| 久久受www免费人成_看片中文 | 欧美国产成人久久精品| 久久99热这里只频精品6| 久久综合成人网| 久久久久久精品成人免费图片| 午夜精品久久影院蜜桃| 亚洲国产精品成人AV无码久久综合影院| 亚洲精品无码专区久久同性男 | 中文国产成人精品久久不卡|