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

            婷婷久久综合| 波多野结衣AV无码久久一区| 久久无码人妻一区二区三区午夜| 国产美女亚洲精品久久久综合| 亚洲人成网亚洲欧洲无码久久| 97r久久精品国产99国产精| 国产日韩久久久精品影院首页| 一本久久a久久精品亚洲| 亚洲欧美日韩精品久久| 中文字幕精品无码久久久久久3D日动漫| 人妻无码精品久久亚瑟影视| 久久青草国产精品一区| 性高湖久久久久久久久| 亚洲欧洲中文日韩久久AV乱码| 国产精品久久久久久久久鸭| 亚洲欧美日韩久久精品第一区| 亚洲综合精品香蕉久久网97| 亚洲中文字幕久久精品无码APP| 久久精品18| 婷婷久久综合九色综合98| 日韩人妻无码精品久久免费一| 欧美精品丝袜久久久中文字幕| 欧美伊香蕉久久综合类网站| 久久久无码精品亚洲日韩按摩 | 亚洲AⅤ优女AV综合久久久| 精品久久无码中文字幕| 国色天香久久久久久久小说| 久久人人爽人人爽人人片AV东京热 | 欧洲人妻丰满av无码久久不卡| 青青热久久国产久精品| 久久久精品人妻无码专区不卡 | 久久精品国产精品亚洲精品| 久久国产乱子伦免费精品| 少妇内射兰兰久久| 狠狠色丁香久久婷婷综合五月| 亚洲国产精品高清久久久| 无码日韩人妻精品久久蜜桃 | 国产成人99久久亚洲综合精品| 国产午夜精品理论片久久影视| 久久精品国产99国产电影网 | 久久人人爽人人人人爽AV |