so true

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

          reinterpret class pointer

          #include <iostream>
          #include <stdlib.h>
          #include <string>
          #include <fstream>
          #include <sstream>
          #include <stdint.h>
          #include <pthread.h>
          #include <vector>
          #include <map>
          #include <set>
          using namespace std;
          #define INIT(x) x = (typeof(x))(long)(&x)
          class Data {
          public:
              virtual ~Data() {};
              Data() {
                  INIT(b);
                  INIT(d);
                  INIT(p);
                  INIT(c);
                  INIT(l);
              }
              bool b;
              double d;
              int* p;
              char c;
              long l;
              bool operator==(const Data& another) const {
                  return another.b == b &&
                         another.d == d &&
                         another.p == p &&
                         another.c == c &&
                         another.l == l;
              }
              bool operator!=(const Data& another) const {
                  return !(*this == another);
              }
              void Print() const {
                  printf("Data::b:%d\n", b);
                  printf("Data::d:%f\n", d);
                  printf("Data::p:%p\n", p);
                  printf("Data::c:%c(%d)\n", c, (unsigned char)c);
                  printf("Data::l:%ld\n", l);
              }
          };
          class Base {
          public:
              Base() {
                  INIT(p);
                  INIT(c);
                  i1 = c + random();
                  i2 = c + random();
                  INIT(d);
                  u3 = c + random();
              }
              virtual ~Base() {};
              void* p;
              char c;
              int i1:1;
              int i2:2;
              double d;
              unsigned int u3:3;
              bool operator==(const Base& another) const {
                  return another.p  == p  &&
                         another.c  == c  &&
                         another.i1 == i1 &&
                         another.i2 == i2 &&
                         another.d  == d  &&
                         another.u3 == u3;
              }
              bool operator!=(const Base& another) const {
                  return !(*this == another);
              }
              virtual void Print() const {
                  printf("Base::p:%p\n", p);
                  printf("Base::c:%c(%d)\n", c, (unsigned char)c);
                  printf("Base::i1:%d\n", i1);
                  printf("Base::i2:%d\n", i2);
                  printf("Base::d:%f\n", d);
                  printf("Base::u3:%u\n", u3);
              }
          };
          class Derive: public Base {
          public:
              Derive() {
                  INIT(i);
                  INIT(c);
                  u1 = i + random();
                  u2 = i + random();
                  u3 = i + random();
                  u4 = i + random();
                  INIT(b);
                  INIT(ui);
                  INIT(uf);
                  INIT(d);
              }
              int i;
              char c;
              unsigned u1:10;
              unsigned u2:10;
              Data dt;
              unsigned u3:10;
              unsigned u4:2;
              bool b;
              union {
                  int ui;
                  float uf;
              };
              double d;
              virtual void Print() const {
                  Base::Print();
                  printf("Derive::i:%d\n", i);
                  printf("Derive::c:%c(%d)\n", c, (unsigned char)c);
                  printf("Derive::u1:%d\n", u1);
                  printf("Derive::u2:%d\n", u2);
                  dt.Print();
                  printf("Derive::u3:%d\n", u3);
                  printf("Derive::u4:%d\n", u4);
                  printf("Derive::b:%d\n", b);
                  printf("Derive::ui:%d\n", ui);
                  printf("Derive::uf:%f\n", uf);
                  printf("Derive::d:%f\n", d);
                  printf("\n");
              }
              bool operator==(const Derive& another) const {
                  return Base::operator ==(another) &&
                         another.i  == i  &&
                         another.c  == c  &&
                         another.u1 == u1 &&
                         another.u2 == u2 &&
                         another.dt == dt &&
                         another.u3 == u3 &&
                         another.u4 == u4 &&
                         another.b  == b  &&
                         another.ui == ui &&
                         another.uf == uf &&
                         another.d  == d;
              }
              bool operator!=(const Derive& another) const {
                  return !(*this == another);
              }
          };
          int main(int argc, char* argv[]) {
              printf("sizeof(Data):%lu\n", sizeof(Data));
              printf("sizeof(Base):%lu\n", sizeof(Base));
              printf("sizeof(Derive):%lu\n", sizeof(Derive));
              printf("\n");
              Derive drv;
              {
                  string s((const char*)&drv, sizeof(Derive));
                  const Derive* p = reinterpret_cast<const Derive*>(s.c_str());
                  if (*p == drv) {
                      cout << "*EQUAL*" << endl;
                      p->Print();
                  }
              }
              drv.i = 1;
              drv.c = 'a';
              drv.u1 = 10;
              drv.u2 = 20;
              drv.u3 = 30;
              drv.u4 = 2;
              drv.b = true;
              drv.ui = 3;
              drv.uf = 3.5;
              drv.d = 8.8;
              {
                  string s((const char*)&drv, sizeof(Derive));
                  const Derive* p = reinterpret_cast<const Derive*>(s.c_str());
                  if (*p == drv) {
                      cout << "*EQUAL*" << endl;
                      p->Print();
                  }
              }
              for (int i = 0; i < 10 * 1024; ++i) {
                  Derive* pdrv = new Derive();
                  {
                      string s((const char*)pdrv, sizeof(Derive));
                      const Derive* p = reinterpret_cast<const Derive*>(s.c_str());
                      if (*p != *pdrv) {
                          cout << "*NOT EQUAL*" << endl;
                          pdrv->Print();
                          p->Print();
                          break;
                      }
                  }
                  delete pdrv;
              }
              return 0;
          }

          ==================     run result     ===================
          sizeof(Data):48
          sizeof(Base):40
          sizeof(Derive):112
          *EQUAL*
          Base::p:0x7fff24993c98
          Base::c:?(160)
          Base::i1:-1
          Base::i2:-2
          Base::d:140733807410344.000000
          Base::u3:1
          Derive::i:614022324
          Derive::c:?(184)
          Derive::u1:295
          Derive::u2:261
          Data::b:1
          Data::d:140733807410384.000000
          Data::p:0x7fff24993cd8
          Data::c:?(224)
          Data::l:140733807410408
          Derive::u3:435
          Derive::u4:2
          Derive::b:1
          Derive::ui:1459617353
          Derive::uf:140733805756416.000000
          Derive::d:140733807410424.000000
          *EQUAL*
          Base::p:0x7fff24993c98
          Base::c:?(160)
          Base::i1:-1
          Base::i2:-2
          Base::d:140733807410344.000000
          Base::u3:1
          Derive::i:1
          Derive::c:a(97)
          Derive::u1:10
          Derive::u2:20
          Data::b:1
          Data::d:140733807410384.000000
          Data::p:0x7fff24993cd8
          Data::c:?(224)
          Data::l:140733807410408
          Derive::u3:30
          Derive::u4:2
          Derive::b:1
          Derive::ui:1080033280
          Derive::uf:3.500000
          Derive::d:8.800000

          posted on 2015-03-04 15:34 so true 閱讀(317) 評論(0)  編輯  收藏 所屬分類: C&C++

          主站蜘蛛池模板: 上林县| 盐山县| 麻城市| 新宾| 宾阳县| 阿城市| 内江市| 云和县| 定陶县| 轮台县| 鄱阳县| 中方县| 渭南市| 德令哈市| 江北区| 马公市| 定州市| 西乌珠穆沁旗| 万年县| 平阳县| 普兰店市| 巴林右旗| 蒙城县| 金塔县| 栾城县| 巴塘县| 三门峡市| 明水县| 巴青县| 舞阳县| 万安县| 邛崃市| 房山区| 正蓝旗| 潢川县| 牟定县| 西宁市| 凤城市| 安庆市| 湖州市| 诏安县|