首页 > 其他 > 详细

reinterpret class pointer

时间:2015-03-05 02:01:45      阅读:279      评论:0      收藏: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

reinterpret class pointer

原文:http://www.blogjava.net/bacoo/archive/2015/03/04/423195.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!