• <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久久国产主播综合精品| 香蕉久久AⅤ一区二区三区| 国产一级持黄大片99久久| 爱做久久久久久| 久久精品中文字幕一区| 色综合久久综合网观看| 欧美精品丝袜久久久中文字幕| 人妻精品久久无码专区精东影业 | 超级97碰碰碰碰久久久久最新| 久久精品人人槡人妻人人玩AV| 精品无码久久久久久国产| 午夜精品久久久久久99热| 久久久久久无码国产精品中文字幕| 国产精品久久久香蕉| 91久久精品视频| 国内精品久久国产大陆| 狠狠色婷婷久久综合频道日韩| 亚洲国产成人久久综合碰碰动漫3d | 久久强奷乱码老熟女网站| 久久国产精品偷99| 91精品国产9l久久久久| 久久精品国产免费观看 | 性欧美大战久久久久久久久| 久久精品国产精品亚洲下载| 久久精品国产只有精品2020| .精品久久久麻豆国产精品| 亚洲精品美女久久久久99| 国内精品人妻无码久久久影院导航| 无码乱码观看精品久久| 久久精品二区| 久久久久久国产精品无码下载| 国产精品丝袜久久久久久不卡| 99久久夜色精品国产网站| 99久久无码一区人妻| 国产视频久久| 久久精品中文字幕第23页| 欧美亚洲日本久久精品| yy6080久久| 国产亚洲综合久久系列| 久久精品国产亚洲一区二区| 国产亚洲美女精品久久久|