• <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>
            隨筆 - 2, 文章 - 0, 評論 - 4, 引用 - 0
            數(shù)據(jù)加載中……

            通過指針訪問類的私有數(shù)據(jù)成員

            我們知道在C++中,為了防止某些數(shù)據(jù)成員或成員函數(shù)從外部被直接訪問,可以將它們聲明為private,這樣編譯器會阻止任何來自外部非友元的直接訪問。

            那么我們真的就沒辦法直接修改類的私有數(shù)據(jù)成員了嗎?

            來看一段代碼:

             1 #include <iostream>
             2 using namespace std;
             3 
             4 
             5 class A
             6 {
             7 private:
             8     // a & b are private members of class A
             9     int a;
            10     double b;
            11 public:
            12     // initialize a & b as value 0
            13     A() : a(0), b(0) {}
            14 
            15 public:
            16     // for display the value of a & b
            17     int GetA();
            18     double GetB();
            19 };
            20 
            21 int A::GetA()
            22 {
            23     return a;
            24 }
            25 
            26 double A::GetB()
            27 {
            28     return b;
            29 }
            30 
            31 int _tmain(int argc, _TCHAR* argv[])
            32 {
            33     A test;
            34 
            35     cout << "before pointer access:\n"
            36          << " test.a = " << test.GetA() << "\n"
            37          << " test.b = " << test.GetB() << "\n" << endl;
            38 
            39     // access object test of class A by pointer
            40     int* privateA = reinterpret_cast<int*>(&Test);
            41     double* privateB = reinterpret_cast<double*>(&Test) + 1;
            42 
            43     // value changing by pointer!
            44     *privateA = 1;
            45     *privateB = 2.5;
            46 
            47     cout << "after pointer access:\n"
            48          << " test.a = " << test.GetA() << "\n"
            49          << " test.b = " << test.GetB() << "\n" << endl;
            50 
            51     return 0;
            52 }
            53 


            運行后輸出結(jié)果為:
            before pointer access:
             test.a = 0
             test.b = 0

            after pointer access:
             test.a = 1
             test.b = 2.5

            可以看到,雖然我們已經(jīng)將 A::a 和 A::b聲明為 private ,但在強大的指針面前,類的任何細節(jié)都暴露無遺。

            某種程度上,類的一個對象可以看作包含不同類型元素的數(shù)組,其數(shù)據(jù)成員的地址偏移由數(shù)據(jù)成員在類定義中的順序決定——類對象的地址指向類中第一個被定義的數(shù)據(jù)成員的地址;第二個被定義的數(shù)據(jù)成員的地址取決于第一個數(shù)據(jù)成員的類型,若第一個為 int 型,則再偏移 4 個字節(jié)( sizeof(int) )即得到第二個數(shù)據(jù)成員的地址(有時也不一定是這樣,如上例中,由于類型對齊的緣故,實際偏移 8 個字節(jié)( sizeof(double) )才得到第二個數(shù)據(jù)成員的地址,具體內(nèi)容不在本文討論范圍內(nèi))。

            所以即使你將其他所有細節(jié)都隱藏在 cpp 中,但頭文件中不可避免地會出現(xiàn)私有數(shù)據(jù)成員的聲明。而類的其他用戶只要能看到這個聲明,就能推算出類中各數(shù)據(jù)成員的相對地址偏移,從而用指針輕松實現(xiàn)對私有成員的訪問。

            上例只對私有數(shù)據(jù)成員進行了驗證,有興趣的可以試試看用函數(shù)指針能否訪問私有成員函數(shù)。

            posted on 2008-08-10 14:06 iwong 閱讀(2765) 評論(4)  編輯 收藏 引用

            評論

            # re: 通過指針訪問類的私有數(shù)據(jù)成員  回復(fù)  更多評論   

            夠狠。
            2008-08-10 14:50 | AlexEric

            # re: 通過指針訪問類的私有數(shù)據(jù)成員  回復(fù)  更多評論   

            不過,我再添點東西,你這個結(jié)論就得改改了。

            5 class A
            6 {
            7 private:
            8 // a & b are private members of class A
            9 int a;
            10 double b;
            11 public:
            12 // initialize a & b as value 0
            13 A() : a(0), b(0) {}
            14
            15 public:
            16 // for display the value of a & b
            17 int GetA();
            18 double GetB();

            //new function
            virtual int f(){return 0;}
            19 };
            2008-08-10 15:01 | AlexEric

            # re: 通過指針訪問類的私有數(shù)據(jù)成員  回復(fù)  更多評論   

            再說,你這個不完全是指針的強大,也是類型強制轉(zhuǎn)換的結(jié)果。。。
            2008-08-10 15:11 | AlexEric

            # re: 通過指針訪問類的私有數(shù)據(jù)成員[未登錄]  回復(fù)  更多評論   

            其實這不過就是根據(jù)地址修改該地址存儲數(shù)據(jù)的值而已,值得大驚小怪么?知道存儲地址了要修改還不是。。。。。。
            2008-08-11 11:40 | dd

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            国产精品久久久久久久午夜片| 国产欧美久久久精品影院| 大香伊人久久精品一区二区| 色99久久久久高潮综合影院| 色青青草原桃花久久综合| 一级女性全黄久久生活片免费 | AV无码久久久久不卡网站下载| 午夜不卡久久精品无码免费| 国产精品美女久久久久久2018| 久久综合丁香激情久久| 久久亚洲电影| 青青草原1769久久免费播放| 国产精品欧美久久久久天天影视| 很黄很污的网站久久mimi色| 国产V综合V亚洲欧美久久| 久久人人超碰精品CAOPOREN | 狠狠色婷婷久久综合频道日韩| 99久久人人爽亚洲精品美女| 久久久久久夜精品精品免费啦| 一本色综合久久| 色噜噜狠狠先锋影音久久| 思思久久99热免费精品6| 久久伊人精品青青草原高清| 久久久久久久精品妇女99| 久久久久久国产精品免费免费| 久久久久免费精品国产| 亚洲精品乱码久久久久久蜜桃图片| 久久精品www| 天天影视色香欲综合久久| 久久婷婷国产麻豆91天堂| 亚洲国产美女精品久久久久∴| 亚洲精品午夜国产va久久| 蜜臀久久99精品久久久久久| 91精品国产91久久久久久蜜臀| 久久久久亚洲AV无码永不| 色88久久久久高潮综合影院| 午夜人妻久久久久久久久| 中文字幕久久亚洲一区| 亚洲国产婷婷香蕉久久久久久| 久久久久久国产精品免费免费| 久久精品三级视频|