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

            久久亚洲欧洲国产综合| 久久久久久精品久久久久| 精品国产一区二区三区久久久狼| 人妻久久久一区二区三区| 国产精品免费久久久久电影网| 久久免费香蕉视频| AAA级久久久精品无码片| 久久国产视屏| 久久精品中文字幕无码绿巨人| 国产精品美女久久久网AV| 亚洲va久久久噜噜噜久久| 色综合久久久久网| 久久妇女高潮几次MBA| 国产999精品久久久久久| 性欧美丰满熟妇XXXX性久久久| 国产精品成人无码久久久久久 | 久久精品亚洲男人的天堂| 久久精品国产亚洲AV影院| 久久久久国产精品| 亚洲va中文字幕无码久久不卡| 久久久精品视频免费观看| 精品久久久噜噜噜久久久| 亚洲精品国产美女久久久| 欧美伊人久久大香线蕉综合69| …久久精品99久久香蕉国产| 久久精品aⅴ无码中文字字幕不卡| 久久精品中文字幕一区| 久久精品国产黑森林| 日韩一区二区久久久久久| 久久精品九九亚洲精品天堂| 久久久无码一区二区三区| 少妇精品久久久一区二区三区| 国产免费久久精品99re丫y| 亚洲精品乱码久久久久久蜜桃| 久久99精品国产麻豆婷婷| 久久99久久成人免费播放| 国产成人精品久久一区二区三区av | 2021国产成人精品久久| 91精品国产综合久久精品| 狠狠88综合久久久久综合网| 波多野结衣中文字幕久久|