当联合成员是整数时,字节顺序会影响联合成员吗?

Does Endian-ness affect the union members when they are integers?

union Chunk
{
  struct { uint32_t index, total; } m_;
  uint64_t m_PlaceHolder;
} chunk;
chunk.m_.index = 1;
chunk.m_.total = 2;
SendOverTCPNetwork(chunk.m_PlaceHolder); // different platform OS will receive this

联合成员设置为 2 个整数,然后通过 TCP 网络发送(组合的)长整数,如上面伪代码所示。

问题:源机器和目标机器的字节顺序是否会影响 chunk 变量的值?

换句话说,我们会在另一端收到相同的值吗?

Will the endian-ness of the source machine & destination machine affect the values of the chunk variable?

是的。字节序影响所有整数,即使它们是 类 的成员。 (当然 signed charunsigned char 除外)。


SendOverTCPNetwork(m_PlaceHolder);

您不能访问没有对象的非静态成员。示例程序格式错误。