使用宏... [英] using macros ...

查看:61
本文介绍了使用宏...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


可能这对你来说可能是一个简单的问题,但我需要一个答案。

在我的软件中,我使用了像OK一样的宏,真,假,失败。一个朋友将

包含此代码作为库存入他的模块中说,在他的代码中

他不能将上述单词用作函数名或变量名。

有编译错误。他建议我用它们作为枚举而不是

宏。他是对的???


(在我的代码中,宏大多被用作fns的返回值)


等待你的回复。


问候,

prakas

Hello,

probably this may be a simple qn to u all but I need an answer plz.
In my software i used macros like OK,TRUE,FALSE,FAILURE . A friend who
included this code as a library into his module said that in his code
he couldnt use the above words as function names or variable names and
got compilation errors. He advised me to use them as enums instead of
macros. is he right ???

(in my code the macros are used as return values from fns mostly)

awaiting your replies.

regards,
prakas

推荐答案

hasadh< h _ *** *@hotmail.com>潦草地写道:
hasadh <h_****@hotmail.com> scribbled the following:
你好,
可能这对你来说可能是一个简单的问题,但我需要一个答案。
在我的软件中,我使用了像OK,TRUE,假,失败。将这段代码作为库包含在他的模块中的朋友说,在他的代码中他不能使用上面的单词作为函数名或变量名,并且有编译错误。他建议我用它们作为枚举而不是
宏。他是对的???
(在我的代码中,宏主要用作fns的返回值)
等待你的回复。
Hello, probably this may be a simple qn to u all but I need an answer plz.
In my software i used macros like OK,TRUE,FALSE,FAILURE . A friend who
included this code as a library into his module said that in his code
he couldnt use the above words as function names or variable names and
got compilation errors. He advised me to use them as enums instead of
macros. is he right ??? (in my code the macros are used as return values from fns mostly) awaiting your replies.




如果这是某种辩论,我愿意支持你。

你的朋友是正确的,他不能使用OK,TRUE,FALSE或FAILURE作为

变量或功能名称,但他为什么要这样做?这是一种常见的拼写

约定,变量和函数名称是小写的,而宏名称的b $ b名称是大写的。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰----- --- \

\ - http:// www.helsinki.fi/~palaste ---------------------规则! -------- /

当一个男人对一个女人说脏话时,那就是性骚扰。当一个女人对男人说脏话时,那是'每分钟14.99美元+本地电话费!'

- Ruben Stiller



If this is some kind of debate, I''m willing to take your side on this.
Your friend is correct that he can''t use OK, TRUE, FALSE or FAILURE as
variable or function names, but why should he? It''s a common spelling
convention that variable and function names are in lower case and macro
names are in upper case.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"When a man talks dirty to a woman, that''s sexual harassment. When a woman talks
dirty to a man, that''s 14.99 per minute + local telephone charges!"
- Ruben Stiller


no****@nospam.inva lid< no **** @ nospam.invalid> ;潦草地写道:
no****@nospam.invalid <no****@nospam.invalid> scribbled the following:
Hello Joona I Palaste,
2003年10月13日,Joona I Palaste< pa ***** @ cc.helsinki.fi>写道:
Hello Joona I Palaste, On 13-Oct-2003, Joona I Palaste <pa*****@cc.helsinki.fi> wrote:


这是一个常见的
拼写
It''s a common spelling


哦!真的吗?


Oh! really?


约定变量和函数名称是小写的,宏名称是大写的。
convention that variable and function names are in lower case and macro
names are in upper case.




我不知道你的问题是什么意思。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \

\ - http://www.helsinki.fi/~palaste ---------------------规则! -------- /

你可以夺走他的生命......

- Mirja Tolsa



I have no clue as to what you are meaning with your question.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You could take his life and..."
- Mirja Tolsa


hasadh写道:
hasadh wrote:
在我的软件中,我使用了像OK,TRUE,FALSE,FAILURE这样的宏。
将此代码作为库包含的朋友进入他的模块
说,在他的代码中,他不能使用上面的单词
作为函数名或变量名并且出现编译错误。
他建议我将它们用作枚举而不是宏。
他是对的吗?
In my software, I used macros like OK, TRUE, FALSE, FAILURE.
A friend who included this code as a library into his module
said that, in his code, he couldn''t use the above words
as function names or variable names and got compilation errors.
He advised me to use them as enums instead of macros.
Is he right?




问题是名称空间污染。

你对全球名称的定义与其他定义。

通过在模块中的所有全局名称前加一个唯一的前缀

,可以轻松解决这个问题:


#define moduleName_OK 1

#define moduleName_FALSE 0

#define moduleName_TRUE(!(moduleName_FALSE))

#define moduleName_SUCCESS 0

#define moduleName_FA ILURE(!(成功))


C预处理器宏是危险的。

枚举,或者更好的是,const全局对象更安全。



The problem is name space pollution.
Your definitions of global names conflict with other definitions.
This problem is easily solved by prepending a unique prefix
to all of the global names in your module:

#define moduleName_OK 1
#define moduleName_FALSE 0
#define moduleName_TRUE (!(moduleName_FALSE))
#define moduleName_SUCCESS 0
#define moduleName_FAILURE (!(SUCCESS))

C preprocessor macros are dangerous.
An enumeration or, better yet, const global objects are much safer.


这篇关于使用宏...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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