这款便携式,符合标准,优雅吗? [英] is this portable, conforming to standard, elegant?

查看:57
本文介绍了这款便携式,符合标准,优雅吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class vector3

{

public:

union

{

float [3]数据;

struct

{

浮动x,y,z;

};

};

};

带数据数组的
访问方便文件io和x,y,z用于其他东西

解决方案

" rz" < gk *** @ hjkjhk.plwrote in news:eq ********** @ nemesis.news.tpi.pl:


class vector3

{

public:

union

{

float [3]数据;

struct

{

浮动x,y,z;

};

};

};

带数据数组的
访问方便文件io和x,y,z用于其他

items



结构是否可能以不同的方式进行字节打包

而不是数组?


RZ写道:


class vector3

{

public:

union

{

float [3]数据;



语法错误。

浮点数据[3];


struct

{

浮动x,y,z;

} ;

};

};

带数据数组的
访问方便文件io和x,y,z用于其他东西



取决于你如何使用它。仅当您访问上次修改的联合的

相同成员时,才会定义行为。换句话说,如果你用
写入x,并尝试用数据[0]读取它,你得到UB。


是吗可能是结构可能以不同的方式进行字节打包


比数组?



您是否询问data []是否始终按此顺序包含值x,y,z?

(如结构中所示)那么答案是肯定的。


class vector3
{
public:
union
{
float[3] data;
struct
{
float x, y, z;
};
};
};

access with data array is convenient for file io and x,y,z for other things

解决方案

"r.z." <gk***@hjkjhk.plwrote in news:eq**********@nemesis.news.tpi.pl:

class vector3
{
public:
union
{
float[3] data;
struct
{
float x, y, z;
};
};
};

access with data array is convenient for file io and x,y,z for other
things


Is it possible that the structure may be byte-packed in a different manner
than an array?


r.z. wrote:

class vector3
{
public:
union
{
float[3] data;

syntax error.
float data[3];

struct
{
float x, y, z;
};
};
};

access with data array is convenient for file io and x,y,z for other things

Depends on how you use it. Behavior is only defined if you access the
same member of the union that you last modified. In other words, if you
write to x, and try to read it with data[0], you get UB.


Is it possible that the structure may be byte-packed in a different manner

than an array?

do you ask whether data[] will always contain values x, y, z in this order?
(like in the structure) then the answer is yes.


这篇关于这款便携式,符合标准,优雅吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆