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

            97精品国产97久久久久久免费| 欧美牲交A欧牲交aⅴ久久| 91精品国产高清久久久久久91| 国产成人久久激情91| 久久精品国产精品亚洲人人| 免费无码国产欧美久久18| 久久久噜噜噜www成人网| 人妻精品久久久久中文字幕| 亚洲精品无码专区久久久| 国产精品成人99久久久久91gav| 香蕉久久影院| 久久精品国产半推半就| 武侠古典久久婷婷狼人伊人| 狠狠色丁香久久婷婷综| 久久人与动人物a级毛片| 国产亚洲成人久久| 久久不见久久见免费视频7| 亚洲精品99久久久久中文字幕| 国产精品美女久久久久久2018| 久久国产成人亚洲精品影院| 久久99精品久久久久久久久久| 久久精品亚洲精品国产欧美| 国内精品伊人久久久久| 一本色道久久综合狠狠躁| 人妻丰满?V无码久久不卡| 久久av免费天堂小草播放| 久久久久夜夜夜精品国产| 久久精品国产亚洲av影院| 久久精品人人做人人爽电影| 久久精品不卡| 狠狠久久综合| 久久国产精品免费| 久久er国产精品免费观看8| 精品久久久久久无码人妻热 | 亚洲av伊人久久综合密臀性色| 亚洲一本综合久久| 日本福利片国产午夜久久| 93精91精品国产综合久久香蕉| 99久久精品国产免看国产一区| 国产午夜福利精品久久2021| 久久国产精品无码一区二区三区|