码迷,mamicode.com
首页 > 其他好文 > 详细

代码。。Bitwise Copy Semantics

时间:2020-05-20 15:52:14      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:include   pac   ret   div   nbsp   hex   输出   ace   short   

#include <bits/stdc++.h>
using namespace std;

struct B {
    B() {
        this->chr = 0xcccc;
    }
    short chr;
};

struct A {
    B s;
    int i;
};

int main() {
    int64_t i64 = 0x1234567890abcedfll;
    A *a = reinterpret_cast<A*>(&i64);
    cout << hex <<  a->s.chr << "," << a->i << "," << i64 << endl;
    int64_t i642 = 0x0;
    A b(*a);
    cout << hex << setw(4) << b.s.chr << "," << b.i << "," << *reinterpret_cast<int64_t*>(&b) << endl;
    return 0;
}

输出结果为:

cedf,12345678,1234567890abcedf
cedf,12345678,1234567890abcedf

如果修改为:

struct B {
    B(const B&) {
        this->chr = 0xcedf;
    }
    short chr;
};

则结果为:

cedf,12345678,1234567890abcedf
cedf,12345678,123456780000cedf

 

代码。。Bitwise Copy Semantics

标签:include   pac   ret   div   nbsp   hex   输出   ace   short   

原文地址:https://www.cnblogs.com/rongminglu/p/12923967.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!