我需要在我的C++程序中共享一些字符串。我应该使用#define还是const字符串?感谢const string与#define
mystring1.h
#define str1 "str1"
#define str2 "str2"
或者
mystring2.h
extern const string str1;
extern const string str2;
mystring.cpp
const string str1 = "str1";
const string str2 = "str2";
预处理器只能用于包含守卫,'__FILE__'和类似的内置宏,以及#ifdefs用于平台特定的常量。虽然你可以使用它来定义你的代码实际使用的常量,但下面的答案指出了几个原因,这是一个坏主意。 – 2011-04-05 00:38:59