结构定义中#define的用途? [英] Purpose of #define inside struct definition?

查看:89
本文介绍了结构定义中#define的用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是一个头文件片段。

这里,#define是struct evConn {}


将它们放在struct block中的任何好处?

看起来在结构块内部和外部

之间的每个标识符的范围内没有差异....


typedef struct evConn {

evConnFunc func;

void * uap;

int fd;

int flags;

#define EV_CONN_LISTEN 0x0001 / * Connection是一个监听器。 * /

#define EV_CONN_SELECTED 0x0002 / * evSelectFD(conn-> file)。 * /

#define EV_CONN_BLOCK 0x0004 / *监听器fd被阻止。 * /

evFileID文件;

struct evConn * prev;

struct evConn * next;

} evConn;


Following is a snippet of a header file.
Here, #define are inside struct evConn{}

Any advantage of putting them inside struct block?
Looks like there is no diff in scoping of each identifier between
inside and outside struct block....

typedef struct evConn {
evConnFunc func;
void * uap;
int fd;
int flags;
#define EV_CONN_LISTEN 0x0001 /* Connection is a listener. */
#define EV_CONN_SELECTED 0x0002 /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK 0x0004 /* Listener fd was blocking. */
evFileID file;
struct evConn * prev;
struct evConn * next;
} evConn;

推荐答案

dj **** @ gmail .com 写道:

以下是头文件的片段。

这里,#define是struct evConn { }


将它们放在struct block中的任何好处?
Following is a snippet of a header file.
Here, #define are inside struct evConn{}

Any advantage of putting them inside struct block?



可能以这种方式编写,以显示标志与结构的

标志成员相关联。

Probably written this way to show the flags are associated with the
flags member of the struct.


看起来在结构块内部和外部

之间的每个标识符的范围内没有差异....


typedef struct evConn {

evConnFunc func;

void * uap;

int fd;

int flags;

#define EV_CONN_LISTEN 0x0001 / * Connection是一个监听器。 * /

#define EV_CONN_SELECTED 0x0002 / * evSelectFD(conn-> file)。 * /

#define EV_CONN_BLOCK 0x0004 / *监听器fd被阻止。 * /

evFileID文件;

struct evConn * prev;

struct evConn * next;

} evConn;
Looks like there is no diff in scoping of each identifier between
inside and outside struct block....

typedef struct evConn {
evConnFunc func;
void * uap;
int fd;
int flags;
#define EV_CONN_LISTEN 0x0001 /* Connection is a listener. */
#define EV_CONN_SELECTED 0x0002 /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK 0x0004 /* Listener fd was blocking. */
evFileID file;
struct evConn * prev;
struct evConn * next;
} evConn;



-

Ian Collins。


--
Ian Collins.


dj **** @ gmail.com 写道:
dj****@gmail.com wrote:

将它们置于struct中的任何优势块?
Any advantage of putting them inside struct block?



Ian所说的......

What Ian said...


#define EV_CONN_LISTEN 0x0001 / * Connection是一个监听器。 * /

#define EV_CONN_SELECTED 0x0002 / * evSelectFD(conn-> file)。 * /

#define EV_CONN_BLOCK 0x0004 / *监听器fd被阻止。 * /
#define EV_CONN_LISTEN 0x0001 /* Connection is a listener. */
#define EV_CONN_SELECTED 0x0002 /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK 0x0004 /* Listener fd was blocking. */



从风格的角度来看,我个人可能更喜欢这些。
#define EV_CONN_LISTEN(0x0001< < 0)/ * Connection是一个监听器。 * /

#define EV_CONN_SELECTED(0x0001<< 1)/ * evSelectFD(conn-> file)。 * /

#define EV_CONN_BLOCK(0x0001<< 2)/ *监听器fd被阻止。 * /


清楚地表明它们是位掩码标志,但我想这个

无论如何都不是你的代码 - 只是一个想法。 (并且看看其他人认为这是多么愚蠢。)


-

C. Benson Manica |我*应该*知道我在说什么 - 如果我

cbmanica(at)gmail.com |不,我需要知道。火焰欢迎。

Just from a style perspective, I might personally prefer these to be

#define EV_CONN_LISTEN ( 0x0001 << 0 ) /* Connection is a listener. */
#define EV_CONN_SELECTED ( 0x0001 << 1 ) /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK ( 0x0001 << 2 ) /* Listener fd was blocking. */

to clearly indicate that they are bitmask flags, but I imagine this
isn''t your code anyway - just a thought. (And to see how silly an
idea others feel this is.)

--
C. Benson Manica | I *should* know what I''m talking about - if I
cbmanica(at)gmail.com | don''t, I need to know. Flames welcome.


5月8日,15:45,Christopher Benson-Manica< a ... @ otaku.freeshell.org>

写道:
On 8 May, 15:45, Christopher Benson-Manica <a...@otaku.freeshell.org>
wrote:

#define EV_CONN_LISTEN 0x0001 / * Connection是一个监听器。 * /

#define EV_CONN_SELECTED 0x0002 / * evSelectFD(conn-> file)。 * /

#define EV_CONN_BLOCK 0x0004 / *监听器fd被阻止。 * /
#define EV_CONN_LISTEN 0x0001 /* Connection is a listener. */
#define EV_CONN_SELECTED 0x0002 /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK 0x0004 /* Listener fd was blocking. */



从风格的角度来看,我个人可能更喜欢这些。
#define EV_CONN_LISTEN(0x0001< < 0)/ * Connection是一个监听器。 * /

#define EV_CONN_SELECTED(0x0001<< 1)/ * evSelectFD(conn-> file)。 * /

#define EV_CONN_BLOCK(0x0001<< 2)/ *监听器fd被阻止。 * /


清楚地表明它们是位掩码标志,但我想这个

无论如何都不是你的代码 - 只是一个想法。 (并且看看其他人认为这是多么愚蠢。)


Just from a style perspective, I might personally prefer these to be

#define EV_CONN_LISTEN ( 0x0001 << 0 ) /* Connection is a listener. */
#define EV_CONN_SELECTED ( 0x0001 << 1 ) /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK ( 0x0001 << 2 ) /* Listener fd was blocking. */

to clearly indicate that they are bitmask flags, but I imagine this
isn''t your code anyway - just a thought. (And to see how silly an
idea others feel this is.)



嗯。我不相信......


如果你有足够的经验来识别位掩码标志,那么你可以认识到2 b的功率对他们来说......


我发现你的班次基础价值看起来有点笨拙,但那只是我的2便士的价值。

Hmmm. I''m not convinced ...

If you''ve got enough experience to recognize bitmask flags, you
probably recognize that powers of 2 map to them...

I find your shift-base values look a little "clunky", but that''s just
my 2p worth.


这篇关于结构定义中#define的用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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