在C ++中使用宏? [英] Use of macros in C++?
问题描述
关于宏的前一个帖子只是提醒我问这个问题。我用
避免使用宏并定义了一些常量,即:
const int BYTES_PER_TRACK = 10 * 256;
const int TRACK0_SS_OFFSET = 0;
const int TRACK0_SS_OFFSET = 10 * 256;
现在,问题是我的项目中有几个模块
引用这些常量,所以我必须在主头文件中使用''
BYTES_PER_TRACK'等中的'extern const',因此常量是
可用于其他模块。
我觉得在这样的情况下使用
宏的intead不是更好吗?或者不是?
感谢您提供的任何见解。
-
http://www.munted.org.uk
小睡一下,它可以挽救生命。 />
A previous thread about macros just reminded me to ask this. I have
avoided using macros and defined some constants i.e.:
const int BYTES_PER_TRACK = 10 * 256;
const int TRACK0_SS_OFFSET = 0;
const int TRACK0_SS_OFFSET = 10 * 256;
Now, the problem is that I have several modules in my project that
references these constants, so I have to use ''extern const in
BYTES_PER_TRACK'' etc., in the main header file so the constants are
available to other modules.
I feel that in a situation like that wouldn''t it be better to use
macros intead? Or not?
Thanks for any insights offered.
--
http://www.munted.org.uk
Take a nap, it saves lives.
推荐答案
* Alex Buell:
* Alex Buell:
关于宏的前一个帖子只是提醒我问这个问题。我已经避免使用宏并定义了一些常量,即:
const int BYTES_PER_TRACK = 10 * 256;
const int TRACK0_SS_OFFSET = 0;
const int TRACK0_SS_OFFSET = 10 * 256;
对名称使用全部大写可能与宏冲突;这是一个坏主意(参见这个小组的常见问题,以及Bjarne Stroustrup的常见问题解答)。
这是一个Java惯例,这不是问题,因为Java不会因为
有一个预处理器。
Java从早期的C中得到它(没有'有'const'',因此
预处理器必须用于常量,其中该约定帮助
以避免与普通的非预处理器符号发生冲突)。
基本上你从历史悠久的时间里迷失了一些非常糟糕的东西,然后用语言的后代语言重新引入它。 >
它的起源 - 一种帮助驱逐怪物的后代语言。
不要盲目地复制惯例和其他东西。
现在,问题是我的项目中有几个模块引用这些常量,所以我必须使用''extern const在
BYTES_PER_T RACK''等,在主头文件中
嗯,其他的不必这样做。
所以常量其他模块可以使用。
它们应该被定义或至少在头文件中声明,是的。
我觉得在这样的情况下不会''使用
宏intead会更好吗?或者不是?
感谢您提供的任何见解。
A previous thread about macros just reminded me to ask this. I have
avoided using macros and defined some constants i.e.:
const int BYTES_PER_TRACK = 10 * 256;
const int TRACK0_SS_OFFSET = 0;
const int TRACK0_SS_OFFSET = 10 * 256;
Use of all uppercase for names is likely to conflict with macros; it''s a
bad idea (see this group''s FAQ, and also Bjarne Stroustrup''s FAQ).
It''s a Java convention, where it isn''t a problem because Java doesn''t
have a preprocessor.
Java got it from early C (which didn''t have ''const'', so that the
preprocessor had to be used for constants, where that convention helped
to avoid conflict with ordinary non-preprocessor symbols).
Essentially you''ve taken something very bad from the mists of historical
time, and reintroduced it in a descendant language of the language where
it originated -- a descendant language that helped to expel the monster.
Don''t copy conventions and other things blindly.
Now, the problem is that I have several modules in my project that
references these constants, so I have to use ''extern const in
BYTES_PER_TRACK'' etc., in the main header file
Well, other don''t have to do that.
so the constants are
available to other modules.
They should be defined or at least declared in a header file, yes.
I feel that in a situation like that wouldn''t it be better to use
macros intead? Or not?
Thanks for any insights offered.
按照上面指示的净指示。
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A :热门发布。
问:usenet和电子邮件中最烦人的事情是什么?
Follow the net directions indicated above.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
2006年5月7日,星期日10:35:45 +0100,Alex Buell
< al ******** @ munted.org.uk>写道:
On Sun, 7 May 2006 10:35:45 +0100, Alex Buell
<al********@munted.org.uk> wrote:
关于宏的前一个帖子只是提醒我问这个问题。我已经避免使用宏并定义了一些常量,即:
const int BYTES_PER_TRACK = 10 * 256;
const int TRACK0_SS_OFFSET = 0;
const int TRACK0_SS_OFFSET = 10 * 256;
现在,问题是我的项目中有几个模块引用这些常量,所以我必须使用''extern const in
BYTES_PER_TRACK''等,在主头文件中,其他模块可以使用常量。
我觉得在这样的情况下使用不会更好
macros intead?或者不是?
A previous thread about macros just reminded me to ask this. I have
avoided using macros and defined some constants i.e.:
const int BYTES_PER_TRACK = 10 * 256;
const int TRACK0_SS_OFFSET = 0;
const int TRACK0_SS_OFFSET = 10 * 256;
Now, the problem is that I have several modules in my project that
references these constants, so I have to use ''extern const in
BYTES_PER_TRACK'' etc., in the main header file so the constants are
available to other modules.
I feel that in a situation like that wouldn''t it be better to use
macros intead? Or not?
考虑使用枚举:
enum {
BYTES_PER_TRACK = 10 * 256 ;
TRACK0_SS_OFFSET = 0;
// ...
};
祝福,
Roland Pibinger
Consider using enums:
enum {
BYTES_PER_TRACK = 10 * 256;
TRACK0_SS_OFFSET = 0;
// ...
};
Best wishes,
Roland Pibinger
On Sun,2006年5月7日10:35:45 +0100 Alex Buell< al ****** **@munted.org.uk>
挥动魔杖,这条消息神奇地出现了:
On Sun, 7 May 2006 10:35:45 +0100 Alex Buell <al********@munted.org.uk>
waved a wand and this message magically appeared:
const int TRACK0_SS_OFFSET = 0;
const int TRACK0_SS_OFFSET = 10 * 256;
const int TRACK0_SS_OFFSET = 0;
const int TRACK0_SS_OFFSET = 10 * 256;
错误,第二个应该是:
const int TRACK0_DS_OFFSET = 10 * 256;
-
http:// www.munted.org.uk
小睡一下,它可以挽救生命。
Err, the second one should be:
const int TRACK0_DS_OFFSET = 10 * 256;
--
http://www.munted.org.uk
Take a nap, it saves lives.
这篇关于在C ++中使用宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!