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

    主站蜘蛛池模板: 免费看美女让人桶尿口| 猫咪免费人成网站在线观看| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 国产福利视精品永久免费| 老司机亚洲精品影院无码| 16女性下面无遮挡免费| 亚洲视频免费在线播放| 免费专区丝袜脚调教视频| 亚洲欧洲高清有无| 妞干网在线免费视频| 精品久久久久久久久亚洲偷窥女厕| 国产真人无遮挡作爱免费视频| 色www免费视频| 国产a v无码专区亚洲av| 国产一级一毛免费黄片| 亚洲国产高清人在线| 日韩版码免费福利视频| 亚洲AV永久无码精品一福利| 日韩亚洲精品福利| 免费国产叼嘿视频大全网站| 91亚洲国产成人精品下载| 性盈盈影院免费视频观看在线一区| 亚洲AV无码一区二区乱子仑 | 国产精品成人四虎免费视频| 无遮挡国产高潮视频免费观看| 国产亚洲成av人片在线观看| 在线成人爽a毛片免费软件| 亚洲中文字幕人成乱码| 国产不卡免费视频| 一级毛片**不卡免费播| 在线aⅴ亚洲中文字幕| 亚洲天堂免费在线视频| 57pao一国产成视频永久免费| 亚洲熟女精品中文字幕| 亚洲人成无码网WWW| 最近中文字幕免费完整 | 久久er国产精品免费观看8| 亚洲第一成年网站大全亚洲| 国产免费拔擦拔擦8x| 国产精品99精品久久免费| 亚洲AV日韩AV无码污污网站|