<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(312) 評論(0)  編輯  收藏 所屬分類: C&C++

    主站蜘蛛池模板: 亚洲精品视频在线观看视频| 亚洲区不卡顿区在线观看| 亚洲综合一区二区精品导航| 成人网站免费大全日韩国产| 亚洲国产精品无码久久青草| 日韩大片免费观看视频播放| 亚洲精品国产综合久久一线| 成人一级免费视频| 亚洲开心婷婷中文字幕| 国产精品偷伦视频观看免费| 亚洲人成电影福利在线播放| 久久九九AV免费精品| 亚洲欧洲自拍拍偷午夜色| 无码国产精品一区二区免费式影视 | 亚洲成AV人片在WWW| 国产免费av一区二区三区| 国产亚洲精品2021自在线| 亚洲美女在线国产| 久久青草免费91线频观看不卡| 亚洲精品国产成人| 好男人视频在线观看免费看片| 色妞www精品视频免费看| 国产亚洲精aa成人网站| 67194成手机免费观看| 亚洲午夜成人精品无码色欲| 亚洲成a人片在线观看日本麻豆 | 又粗又硬免费毛片| 97在线视频免费公开视频| 久久精品国产亚洲av水果派| 成年女人免费视频播放77777| 欧洲亚洲国产精华液| 激情97综合亚洲色婷婷五 | 精品韩国亚洲av无码不卡区| 亚洲人成网77777亚洲色| 97性无码区免费| 污网站在线免费观看| 久久精品国产亚洲av高清漫画| 麻豆国产入口在线观看免费| a级黄色毛片免费播放视频| 亚洲人成777在线播放| 免费人成年轻人电影|