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

            精品国产一区二区三区久久| 久久婷婷五月综合色99啪ak| 亚洲另类欧美综合久久图片区| 亚洲国产成人精品无码久久久久久综合| 大美女久久久久久j久久| 久久se这里只有精品| 久久综合狠狠综合久久综合88| aaa级精品久久久国产片| 久久乐国产精品亚洲综合| 久久久久人妻一区二区三区 | 亚洲欧洲精品成人久久曰影片| 手机看片久久高清国产日韩| 久久精品国产99久久久| 免费久久人人爽人人爽av| 99久久伊人精品综合观看| 免费精品久久天干天干| 狠狠精品久久久无码中文字幕| 天堂久久天堂AV色综合| 人妻无码精品久久亚瑟影视| 成人妇女免费播放久久久| 久久久久久无码Av成人影院| 久久九九久精品国产免费直播| 国产精品内射久久久久欢欢| 97久久国产综合精品女不卡| 亚洲国产精品无码久久九九| 久久99精品久久久久久| 波多野结衣AV无码久久一区| 久久se精品一区二区影院| 成人国内精品久久久久影院VR| 久久久久久无码Av成人影院| 亚洲精品乱码久久久久久| 大香伊人久久精品一区二区| 热久久国产欧美一区二区精品| 久久99精品久久久久久野外 | 久久精品亚洲一区二区三区浴池| 人妻无码精品久久亚瑟影视| 久久伊人五月天论坛| 天天影视色香欲综合久久| 爱做久久久久久| 久久久亚洲精品蜜桃臀| 亚洲一级Av无码毛片久久精品|