简体   繁体   English

定义具有相同名称和值的符号字符串常量

[英]Defining symbolic string constants which have the same name and value

I have a situation where I've got a number of strings that I want to be able to use symbolically, ie as if I'd declared a macro 我遇到的情况是,我想使用一些可以象征性使用的字符串,即好像我声明了一个宏

#define kBlahProperty "kBlahProperty"
#define kFooProperty "kFooProperty"

There are various reasons why I can't use an enum (for more context see this previous question ), such as getting useful info in the debugger, serialising data in a readable format and dealing with sets of properties defined in different source files. 我不能使用枚举的原因有多种(有关更多上下文,请参见前面的问题 ),例如在调试器中获取有用的信息,以可读格式序列化数据以及处理在不同源文件中定义的属性集。

Typical usage: 典型用法:

setProperty(kBlahProperty, 6);
if (otherProperty==kBlahProperty) { doStuff(); }
std::cout << "property type: " << kBlahProperty;  // outputs "kBlahProperty" rather than an enum

There may be many of these properties, and to ease definition I'd like to be able to declare them like this, in one place only: 这些属性可能很多,为了简化定义,我希望能够在一个地方这样声明它们:

DEFINE_PROPERTY(kBlahProperty)
DEFINE_PROPERTY(kFooProperty)

However, I can't find a way of defining a macro that will itself expand to #define (I've tried a couple of examples with the '#' and '##' operators, but getting to paste the '#define' is the problem). 但是,我找不到定义本身可以扩展为#define的宏的方法(我已经尝试了一些使用'#'和'##'运算符的示例,但是要粘贴'#define'是问题)。

An alternative tack was to define the macro to expand as a char* : 另一种方法是定义要扩展为char*的宏:

#define DEFINE_PROPERTY(propName) const char8* propName=#propName

but this has the problem that it gets defined in each compilation unit and hence linker errors. 但这有一个问题,即在每个编译单元中都定义了它,因此产生了链接器错误。 If I change it to use extern then I can't define it to use the string itself -- that has to go in the .cpp file and so now I would need to declare all these properties in the header and define them again in the cpp file, so you have a list in two places. 如果我将其更改为使用extern则无法将其定义为使用字符串本身-必须放在.cpp文件中,因此现在我需要在标头中声明所有这些属性,然后在cpp文件,因此您在两个位置都有一个列表。

So is there a good way of defining these strings so that you can just declare them once? 因此,有没有一种好的方法来定义这些字符串,以便您只需声明一次即可? I have access to boost preprocessor, but only to a small subset of C++-0x (so in-class constant initialisers are out). 我可以使用Boost预处理器,但只能访问C ++-0x的一小部分(因此无法使用类内常量初始化程序)。

You can define your macro to have and #ifdef so it has two paths - if and else. 您可以将宏定义为具有#ifdef,以便它具有两条路径-if和else。 Then in the header it just declares your propName. 然后在标头中仅声明您的propName。 Then in your .cpp file, define a preprocessor variable before you include your header. 然后,在.cpp文件中,定义一个预处理程序变量,然后再添加头文件。 That define will make your macro define the propName. 该定义将使您的宏定义 propName。

Simple (if ugly) way -- define the macro to include extern only if another symbol is not defined, and define that symbol in exactly one compilation unit before you include the header 简单(如果很丑)-仅在未定义另一个符号的情况下,才将宏定义为包括extern,并在包含头文件之前以一个完整的编译单元定义该符号

With example (include guards omitted for clarity) 带示例(为清楚起见,省略了防护罩)

#ifdef IN_CPP
#define DEFINE_PROPERTY(propName) const char8* propName=#propName;
#else
#define DEFINE_PROPERTY(propName) extern const char8* propName;
#endif

In exactly one compilation unit: 仅在一个编译单元中:

#define IN_CPP 1
#include "header.h"

In remaining compilation units: 在其余编译单元中:

#include "header.h"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM