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

            模特私拍国产精品久久| 久久国产精品无码HDAV| 久久国产欧美日韩精品| 久久精品麻豆日日躁夜夜躁| 久久福利青草精品资源站免费| 久久久99精品成人片中文字幕 | 成人综合久久精品色婷婷| 日产精品99久久久久久| 久久亚洲高清综合| 精品久久一区二区三区| 午夜精品久久久久久久无码| 中文字幕亚洲综合久久| 无码人妻精品一区二区三区久久久 | 亚洲欧美日韩中文久久| 国产午夜电影久久| 欧美精品丝袜久久久中文字幕| 日本强好片久久久久久AAA| 国产精品日韩欧美久久综合| 久久久av波多野一区二区| 日韩亚洲国产综合久久久| 久久亚洲国产欧洲精品一| 国产精品毛片久久久久久久| 久久久久亚洲av无码专区 | 久久综合精品国产二区无码| 青青热久久国产久精品| 大香网伊人久久综合网2020| 91精品国产综合久久久久久| 伊人久久精品无码av一区| 亚洲综合精品香蕉久久网| 久久综合五月丁香久久激情| 色综合久久最新中文字幕| 国产精品久久网| 久久综合综合久久97色| 久久伊人精品青青草原高清| 国产日产久久高清欧美一区| 国产∨亚洲V天堂无码久久久| 精品久久久久久久无码| 久久久久久亚洲AV无码专区| 久久久久亚洲AV无码专区体验| 国产精品无码久久久久久| 99久久国产综合精品麻豆|