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

          主站蜘蛛池模板: 清涧县| 香格里拉县| 定襄县| 怀远县| 鹤岗市| 南丹县| 荃湾区| 淮安市| 宁都县| 郎溪县| 波密县| 宜黄县| 土默特左旗| 门源| 乌什县| 离岛区| 南宫市| 潞城市| 吕梁市| 南靖县| 涡阳县| 镇巴县| 邹城市| 陈巴尔虎旗| 大厂| 潼南县| 扎兰屯市| 库尔勒市| 望谟县| 新疆| 武冈市| 伊宁县| 新沂市| 贺州市| 宜兰县| 凌源市| 寿宁县| 沛县| 永丰县| 原阳县| 莲花县|