阅读背景:

有人可以解释一下联合在这行代码中是如何工作的以及数字是如何被转换的吗?

来源:互联网 
#include<iostream>
using namespace std;
union swap_byte {                //This code is  for union 
public:
    void swap();
    void show_byte();
    void set_byte(unsigned short x);
    unsigned char c[2];
    unsigned short s;
};

void swap_byte::swap()            //swaping the declared char c[2]
{
    unsigned char t;
    t = c[1];
    c[1] = c[0];
    c[0] = t;
}
void swap_byte::show_byte()
{
    cout << s << "\n";
}
void swap_byte::set_byte(unsigned short x)        //input for the byte 
{
    s = x;
}
int main()
{
    swap_byte b;
    b.set_byte(49034);
    b.show_byte();
    b.swap();
    b.show_byte();
    cin.get();
    return 0;
}
#include<iostream>
using namespace std;
union s



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: