• <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 閱讀(217) 評論(0)  編輯 收藏 引用 所屬分類: Questions

            一本一本久久a久久精品综合麻豆| 久久精品国产精品亚洲毛片| 精品水蜜桃久久久久久久| 久久久久一级精品亚洲国产成人综合AV区 | 久久婷婷五月综合国产尤物app | 国产亚洲精久久久久久无码| 久久免费国产精品一区二区| 亚洲欧美久久久久9999| 99久久无码一区人妻a黑| 久久AAAA片一区二区| 国产婷婷成人久久Av免费高清 | 久久精品国产亚洲Aⅴ蜜臀色欲| 2021国内久久精品| 国产福利电影一区二区三区,免费久久久久久久精| 久久综合一区二区无码| 久久国产精品99精品国产987| 97视频久久久| 亚洲国产精品成人久久蜜臀 | 久久久亚洲精品蜜桃臀| 久久A级毛片免费观看| 久久久久久免费视频| 久久久久无码精品| 中文精品久久久久国产网址| 久久久久久人妻无码| 东方aⅴ免费观看久久av| 最新久久免费视频| 亚洲国产成人精品女人久久久| 色成年激情久久综合| AV无码久久久久不卡蜜桃| 亚洲日本va中文字幕久久| 人妻系列无码专区久久五月天| 国产精品丝袜久久久久久不卡| 精品国产VA久久久久久久冰| 久久亚洲私人国产精品vA| 国产精品久久午夜夜伦鲁鲁| 亚洲色大成网站www久久九| 日韩人妻无码精品久久免费一| 热re99久久精品国99热| 久久久久人妻一区精品性色av| 午夜天堂av天堂久久久| 久久99精品久久久久久久久久|