当其他宏引发名称冲突时,如何使用其他宏定义宏

编程入门 行业动态 更新时间:2024-10-25 08:18:32
本文介绍了当其他宏引发名称冲突时,如何使用其他宏定义宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是对此的后续问题(请仔细阅读我的问题):如何避免名称冲突来自#用C定义?(或C ++)假设我#define宏ROW和COL.然后,我使用ROW和COL #define ARRSIZE.然后,我声明一个静态数组,例如 float myarray [ARRSIZE]; .现在,如果我修改ROW和COL,则静态数组大小将相应更改.但是在我的特殊情况下,ROW和COL的名称与我在同一文件中使用的结构类型的成员名称冲突.有人告诉我使用const变量,而不要使用'#define'以避免冲突.我喜欢这个主意,并修改了代码,如下所示(这是一个示例).

This is a follow-up question to this (please read my question to the end) : how to avoid name conflicts coming from #define in C? (or C++) Suppose that I #define macros ROW and COL. I then #define ARRSIZE using ROW and COL. Then, I declare a static array like float myarray[ARRSIZE];. Now if I modify ROW and COL, the static array size changes accordingly. But in my special case, the names of ROW and COL conflict with a member name of a struct type I'm using in the same file. Someone told me to use a const variable instead of using '#define' to avoid the conflict. I liked the idea and modifed the code as shown below (it's an example).

const int ROW = 100; const int COL = 200; const int ARRSIZE = ROW*COL; float myarray[ARRSIZE];

通过编译可以得到我

error: expected '=', ',', ';', 'asm' or '__attribute__' before 'ARRSIZE'

在我定义ARRSIZE的那一行.当然,我可以在函数内部使用 int ARRSIZE = ROW * COL; 并使用malloc动态分配函数内部的数组.但是,如果我只想更改ROW和COL而又不想使用malloc怎么办?当然,使用#define进行操作不会有任何问题(名称冲突除外).

at the line where I define ARRSIZE. Of course I can use int ARRSIZE = ROW*COL; inside a function and dynamically allocate the array inside the function with malloc. But what if I want to change only ROW and COL and don't want to use malloc? Of course, there is no problem (except the name conflicts) when doing it with #define.

#define ROW 100 #define COL 200 #define ARRSIZE ROW*COL float myarray[ARRSIZE];

所以有一个问题:我只想更改ROW,COL,并希望自动更改静态数组的大小,但是同时,ROW和COL在同一源文件中显示为结构的成员变量.我无法确定结构成员的名称.总之,我想定义一个不应用于成员函数/变量或全局变量的类似宏的值".在我的情况下,最好的做法是什么?只是将变量ROW和COL更改为特殊的东西?问了这个问题后,我得到的印象是,这可能是唯一的解决方案..:)

So the problem: I want to change only ROW, COL and want the static array size automatically changed, but at the same time, ROW and COL appear as a member variable of a struct in the same source file. I cannot fix the name of the struct member. So in a word, I want to define a 'macro like values' that are not applied to a member function/variable nor global variables. What is the best practice to do it in my case? Just changing the variables ROW and COL to something special? Having asked this question, I get an impression that, that may be the only solution.. :)

推荐答案

编译注释以给出答案:

解决方案A,如果您由于某种原因不得不在代码中坚持使用"ROW"和"COL"(获得美尔康宁的积分):

Solution A, if you for some reason have to stick with "ROW" and "COL" in your code (credits to melpomene):

enum { ROW = 100 }; enum { COL = 200 }; enum { ARRSIZE = ROW*COL };

解决方案B,如果您可以自由选择标识符;为将来的重用提供更强大的功能:通过选择不同的,非冲突的,更长的标识符来避免命名冲突.我对简短,明显的标识符不屑一顾,您遇到的冲突就是一个很好的例子.其他例子使我的雇主花费了很多时间和金钱.

Solution B, if you are free to choose identifiers; more robust for future reuse: Avoid the naming conflict by choosing different, non-conflicting, longer identfiers. I have a superstitious distrust of short, obvious identifiers, the conflict you encountered is a good example why. Other examples have cost my employers quite some time and money.

(我发现其他地方的美洛美犬对答案的声誉不再感兴趣,非常无私.我认为值得在这里做一个不错的Q/A配对,而从美洛美尼身上吸取任何东西.)

更多推荐

当其他宏引发名称冲突时,如何使用其他宏定义宏

本文发布于:2023-11-23 18:28:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622450.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   冲突   定义   名称

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!