so true

          心懷未來,開創未來!
          隨筆 - 160, 文章 - 0, 評論 - 40, 引用 - 0
          數據加載中……

          C++中突破private的方法

          突破private的方法:
          =====================================
          對于private變量:
          方法一:
          class Y
          {
          public:
           Y(int y=0):m_n(y){};
           int getInt(){return m_n;}
          private:
           int m_n;
          };

          Y y(34);
          cout<<y.getInt()<<endl;
          int* pi=(int*)(&y);
          *pi=347;
          cout<<y.getInt()<<endl;
          方法二:
          int tmp;
          __asm
          {
           mov eax,y.m_n
           mov tmp,eax
           add y.m_n,5
          }
          cout<<tmp<<' '<<y.getInt()<<endl;
          ========================================
          對于private函數:
          class C1
          {
          public:
           virtual void test()
           {
            cout<<"public C1::test()"<<endl;
           }
          };

          class C2: public C1
          {
          private:
           virtual void test()
           {
            cout<<"private C2::test()"<<endl;
           }
          };

          C2 c2;
          C1 * pC1=&c2;
          pC1->test();
          方法二:
          class O;
          typedef void (O::*pO)();

          class O
          {
          public:
           static pO getFunTest()
           {
            return &O::test;
           }
          private:
           void test()
           {
            cout<<"this is a private function of class O:test()"<<endl;
           }
          };
          pO pp=O::getFunTest();
          O o;
          (o.*pp)();

          posted on 2008-07-10 15:58 so true 閱讀(434) 評論(0)  編輯  收藏 所屬分類: C&C++

          主站蜘蛛池模板: 广东省| 金门县| 南宁市| 西青区| 社旗县| 宁晋县| 八宿县| 白山市| 巴塘县| 西青区| 庐江县| 东安县| 小金县| 应城市| 酒泉市| 南阳市| 江达县| 广灵县| 梓潼县| 平度市| 瑞金市| 九龙城区| 富蕴县| 青川县| 洛扎县| 宜宾县| 无锡市| 西青区| 洛宁县| 临汾市| 红河县| 新乡县| 陕西省| 崇义县| 旬邑县| 乌兰县| 莆田市| 宝丰县| 泸定县| 江孜县| 旬邑县|