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

            醬壇子

            專注C++技術(shù) 在這里寫下自己的學(xué)習(xí)心得 感悟 和大家討論 共同進(jìn)步(歡迎批評!!!)

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              66 Posts :: 16 Stories :: 236 Comments :: 0 Trackbacks

            公告

            王一偉 湖南商學(xué)院畢業(yè) 電子信息工程專業(yè)

            常用鏈接

            留言簿(19)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            積分與排名

            • 積分 - 387834
            • 排名 - 64

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            inheritance的重要性質(zhì)就是你可以通過指向base class objects的pointers或者references來操作derived class object.

            同樣C++也允許你使用pointers或者references來操作derived class object形成的數(shù)組。但是這樣的行為很危險(xiǎn),幾乎不會(huì)按你設(shè)定的方式運(yùn)作。

            我們來舉個(gè)例子:

            ?class?A{};
            ?class?B:public?A{};

            ?//現(xiàn)在我們有一個(gè)函數(shù)
            void?printAArray(ostream&?s,const?A?array[],int?numElement)
            {
            ???
            ??? for(int?i=0;i<numElement;++i)
            ??????
            {
            ??????????? s
            <<array[i];//假設(shè)A?objects中有一個(gè)operator<<可以用?
            ?
            ???? }

            }


            當(dāng)你把A AArray[10];傳給他
            ...
            printAArray(cout,AArray,10);//運(yùn)作良好

            但是當(dāng)你把B BArray傳給他
            printAArray(cout,BArray,10);//還能正常運(yùn)作嗎

            毫無疑問,編譯時(shí)編譯器會(huì)很開心的接受他。但是運(yùn)行時(shí)呢
            ?for(int?i=0;i<numElement;++i)
            ??
            {
            ??????????? s
            <<array[i];//假設(shè)A?objects中有一個(gè)operator<<可以用?
            ?
            }
            就會(huì)出問題,array所指向的內(nèi)存和array+i所指向的內(nèi)存有多遠(yuǎn)的距離
            答案是i*sizeof(A),但是我們在穿BArray的時(shí)候我們需要的是i*sizeof(B),因?yàn)?br />derived class object通常比base class object 大。

            同樣你通過bass class來刪除derived class object的時(shí)候,都會(huì)產(chǎn)生不可預(yù)期的錯(cuò)誤

            簡單的說,大家需要注意的一點(diǎn)就是,數(shù)組不要和多態(tài)進(jìn)行混合使用,因?yàn)閿?shù)組總是會(huì)涉及指針
            的算術(shù)運(yùn)算。

            當(dāng)然這個(gè)也不是絕對的,都是內(nèi)存訪問惹的禍

            如果你能夠避免一個(gè)具象類(帶有參數(shù)的類)繼承自另外的一個(gè)具象類,你就不太能犯
            下這樣的錯(cuò)誤。

            具體的內(nèi)容我晚上在下一篇隨筆里面進(jìn)行介紹
            posted on 2007-01-31 09:44 @王一偉 閱讀(1423) 評論(7)  編輯 收藏 引用

            Feedback

            # re: 不要使用polymorphically方式處理數(shù)組 2007-01-31 23:23 Elvis
            個(gè)人覺得這是一個(gè)典型的對象切片~A AArray[10]中的10個(gè)A對象的實(shí)例是分配在棧上的靜態(tài)對象,而不是分配在堆上的動(dòng)態(tài)對象吧~這樣的話其實(shí)就不存在多態(tài)了,因?yàn)槎鄳B(tài)是對指針對象而言的,于是當(dāng)將子類對象賦給基類對象時(shí),對象切片就發(fā)生了~  回復(fù)  更多評論
              

            # re: 不要使用polymorphically方式處理數(shù)組 2007-02-01 11:23 王一偉
            不是吧,棧內(nèi)我還是可以調(diào)用函數(shù)的,再入棧一次就是了,我仍然可以使用指針方式進(jìn)行參數(shù)傳遞。
            確實(shí),這個(gè)是一個(gè)切片現(xiàn)象。
            呵呵
            老兄 發(fā)覺這人好少啊 我想搬去csdn了 那人好多  回復(fù)  更多評論
              

            # re: 不要使用polymorphically方式處理數(shù)組 2007-02-02 01:08 OOMusou
            Elvis說的沒錯(cuò)
            很典型的Object Slicing
            請看Effective C++ 條款20  回復(fù)  更多評論
              

            # re: 不要使用polymorphically方式處理數(shù)組 2007-02-02 09:15 holyfire
            這根本不是多態(tài)的運(yùn)用方式,C++下使用多態(tài)要用基類指針的方式,看來你還需要加強(qiáng)下基礎(chǔ)  回復(fù)  更多評論
              

            # re: 不要使用polymorphically方式處理數(shù)組 2007-02-02 11:41 王一偉
            恩,我馬上去瞧瞧Effective C++ 條款20,汗 我還沒看到那

            但是這個(gè)不是多態(tài)需要的基類指針的方式我就表示懷疑了,
            只要virtual了base class的destructor 感覺后面的問題就可以解決了

            多謝大家關(guān)心,不過還是希望大家能詳細(xì)點(diǎn)告訴我錯(cuò)在哪里 嘿嘿 基礎(chǔ)差偶?xì)g迎大家批評 呵呵 最近惡補(bǔ)基礎(chǔ)


              回復(fù)  更多評論
              

            # re: 不要使用polymorphically方式處理數(shù)組 2007-02-03 01:52 Elvis
            PS:你說的這個(gè)問題在C++ CODING STANDARDS的第100條有論述~  回復(fù)  更多評論
              

            # re: 不要使用polymorphically方式處理數(shù)組 2007-02-03 09:03 醬菜
            呵呵 是的 Elvis兄

            Don't treat arrays polymorphically

            Pointers serve two purposes at the same time: that of monikers (small identifiers of objects), and that of array iterators (they can walk through arrays of objects using pointer arithmetic). As monikers, it makes a lot of sense to treat a pointer to Derived as a pointer to Base. As soon as the array iteration part enters the stage, however, such substitutability breaks down because an array of Derived isn't the same as an array of Base. To illustrate: Mice and elephants are both mammals, but that doesn't mean a convoy of a thousand elephants would be as long as one of a thousand mice.

            Size does matter. When substituting a pointer to Derived to a pointer to Base, the compiler knows exactly how to adjust the pointer (if necessary) because it has enough information about both classes. However, when doing pointer arithmetic on a pointer p to Base, the compiler computes p[n] as *(p + n * sizeof(Base)), thus assuming that the objects lying in memory are all Base objectsand not objects of some derived type that might have a different size. Imagine, now, just how easy it is to tromp all over of an array of Derived if you convert the pointer marking its start to Base* (with compiler's silent approval) and then perform pointer arithmetic on that pointer (while the compiler doesn't blink an eye either)!

            Such accidents are an unfortunate interaction between substitutability, which dictates that pointers to derived classes should be usable as pointers to their bases, and C's legacy pointer arithmetic, which assumes pointers are monomorphic and uses solely static information to compute strides.

            To store arrays of polymorphic objects, you need an array (or, better still, a real container; see Item 77) of pointers to the base class (e.g., plain pointers or, better still, shared_ptrs; see Item 79). Then each pointer in the array refers to a polymorphic object, likely an object allocated on the free store. Or, if you want to expose an interface to a container of polymorphic objects, you need to encapsulate the entire array and offer a polymorphic interface for iteration.

            Incidentally, a good reason to prefer references to pointers in interfaces is to make it clear that you're talking about one object, as opposed to possibly an array of them.
              回復(fù)  更多評論
              

            www性久久久com| 亚洲国产精久久久久久久| 综合久久给合久久狠狠狠97色| 欧洲成人午夜精品无码区久久| 99久久久久| 亚洲女久久久噜噜噜熟女| 久久99热这里只有精品66| 狠狠人妻久久久久久综合| 久久亚洲精品中文字幕三区| 久久精品一本到99热免费| 人妻精品久久久久中文字幕| 国产福利电影一区二区三区久久老子无码午夜伦不 | 欧美一区二区久久精品| 国产69精品久久久久99| 99久久综合国产精品免费| 伊人色综合久久| 久久久久久无码Av成人影院| 伊人久久亚洲综合影院| 色综合久久综精品| 丰满少妇人妻久久久久久| 精品久久久无码人妻中文字幕| 女人高潮久久久叫人喷水| 99久久夜色精品国产网站| 看久久久久久a级毛片| 2021国产精品午夜久久| 婷婷久久综合| 中文成人无码精品久久久不卡| 久久久国产精品| 日本欧美久久久久免费播放网| 亚洲国产成人精品无码久久久久久综合| 久久夜色tv网站| 亚洲国产精品久久| 狠狠色综合网站久久久久久久| 久久综合综合久久狠狠狠97色88| 久久久久亚洲精品无码蜜桃| 人妻无码αv中文字幕久久| 狠狠色婷婷久久综合频道日韩| 国产精品久久久天天影视| 看全色黄大色大片免费久久久| 亚洲色婷婷综合久久| 伊人久久大香线蕉综合Av|