将成员数据集处理为数组 [英] treating set of member data as an array

查看:56
本文介绍了将成员数据集处理为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我刚刚遇到以下代码:


struct Vector

{

浮动x,y,z;


inline float& operator [](size_t i)

{

断言(i< 3);

return *(& x + i); < br $>
}

};


i认为它可以在内存中放置,但是我想问一下/>


是否是一个好的和安全的编程实践。


非常感谢提前,

mojmir

hello,

i''ve just encountered following piece of code:

struct Vector
{
float x, y, z;

inline float & operator[] (size_t i)
{
assert(i<3);
return *(&x + i);
}
};

i think it can work due to placement in memory, but i''d like to ask
you
whether it is a good and safe programming practice or not.

many thanks in advance,
mojmir

推荐答案

mojmir写道:
mojmir wrote:

hello,


我刚刚遇到以下代码:


struct Vector

{

浮动x,y,z;


inline float& operator [](size_t i)

{

断言(i< 3);

return *(& x + i); < br $>
}

};


i认为它可以在内存中放置,但是我想问一下/>


是否是一个好的和安全的编程实践。


非常感谢提前,

mojmir
hello,

i''ve just encountered following piece of code:

struct Vector
{
float x, y, z;

inline float & operator[] (size_t i)
{
assert(i<3);
return *(&x + i);
}
};

i think it can work due to placement in memory, but i''d like to ask
you
whether it is a good and safe programming practice or not.

many thanks in advance,
mojmir



唯一的事实是你不知道这是不是一个好习惯,这意味着

不是一个好的做法(即,在可以避免的情况下增加复杂性)。

无论如何,afaik是不安全的,因为对于x来说,对于
的要求是对的, y,z。


问候,


Zeppe

the only fact that you don''t know if it''s a good practice, it means that
is not a good practice (i.e., adds complexity where it can be avoided).
Anyway, afaik is not safe due to the fact that there cen be alignement
requirements for x, y, z.

Regards,

Zeppe


2007-10 -25 14:23,mojmir写道:
On 2007-10-25 14:23, mojmir wrote:

你好,


我刚刚遇到以下代码:


struct Vector

{

浮动x,y,z;


inline float& operator [](size_t i)

{

断言(i< 3);

return *(& x + i); < br $>
}

};


i认为它可以在内存中放置,但是我想问一下/>
你是否是一个好的和安全的编程实践。
hello,

i''ve just encountered following piece of code:

struct Vector
{
float x, y, z;

inline float & operator[] (size_t i)
{
assert(i<3);
return *(&x + i);
}
};

i think it can work due to placement in memory, but i''d like to ask
you whether it is a good and safe programming practice or not.



重要的词是* can *。上面的代码可以在

内存中放置,但由于放置在内存中,它也无法工作。更安全的

版本可能如下所示:


struct vector

{

float x, y,z;

浮动& operator [](size_t i)

{

断言(0< = i&& i< 3);

返回我< 2?我== 0? x:y:z;

}

};


[]运算符的主体可以替换为if-声明,如果你更好的是



-

Erik Wikstr ?? m

The important word is *can*. The above code can work due to placement in
memory, but it can also not work due to placement in memory. A safer
version could look like this:

struct vector
{
float x, y, z;
float& operator[](size_t i)
{
assert(0 <= i && i <3);
return i < 2 ? i == 0 ? x : y : z;
}
};

The body of the [] operator can be replaced with if-statements if you
like that better.

--
Erik Wikstr??m


10月25日下午4:24,Erik Wikstr?m< Erik-wikst ... @ telia.comwrote:
On Oct 25, 4:24 pm, Erik Wikstr?m <Erik-wikst...@telia.comwrote:

On 2007-10-25 14:23,mojmir写道:


On 2007-10-25 14:23, mojmir wrote:



hello,
hello,


我刚刚遇到以下代码:
i''ve just encountered following piece of code:


struct Vector

{

浮动x,y,z;
struct Vector
{
float x, y, z;


inline float& operator [](size_t i)

{

断言(i< 3);

return *(& x + i); < br $> b $ b}

};
inline float & operator[] (size_t i)
{
assert(i<3);
return *(&x + i);
}
};


i认为它可以在内存中放置,但我想询问

你是否是一个好的和安全的编程实践与否。
i think it can work due to placement in memory, but i''d like to ask
you whether it is a good and safe programming practice or not.



重要的词是* can *。上面的代码可以在

内存中放置,但由于放置在内存中,它也无法工作。更安全的

版本可能如下所示:


struct vector

{

float x, y,z;

浮动& operator [](size_t i)

{

断言(0< = i&& i< 3);

返回我< 2?我== 0? x:y:z;

}


};


[]运算符的主体可以是如果你更好地用if-statements代替




-

Erik Wikstr?m-隐藏引用的文字 -


- 显示引用文字 -


The important word is *can*. The above code can work due to placement in
memory, but it can also not work due to placement in memory. A safer
version could look like this:

struct vector
{
float x, y, z;
float& operator[](size_t i)
{
assert(0 <= i && i <3);
return i < 2 ? i == 0 ? x : y : z;
}

};

The body of the [] operator can be replaced with if-statements if you
like that better.

--
Erik Wikstr?m- Hide quoted text -

- Show quoted text -



等一下!为什么不只是一个简单的数组呢?


enum {x = 0,y = 1,z = 2};

typedef float vec3d [z + 1] ;


vec3d v = {1,2,3};

{//在某个函数中

v [x] = ....

v [y] = ...

v [z] = ...

}

谢谢,

FM。

Wait a sec!!! why not just a simple array?

enum{x=0,y=1,z=2};
typedef float vec3d[z+1];

vec3d v={1,2,3};
{//in some function
v[x]=....
v[y]=...
v[z]=...
}

thanks,
FM.


这篇关于将成员数据集处理为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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