C++ const 字符串与#define

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5545570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:24:31  来源:igfitidea点击:

const string vs. #define

c++

提问by user612308

i need to share some strings in my c++ program. should i use #define or const string? thanks

我需要在我的 C++ 程序中共享一些字符串。我应该使用 #define 还是 const 字符串?谢谢

mystring1.h

mystring1.h

#define str1 "str1"
#define str2 "str2"    

Or
mystring2.h


mystring2.h

extern const string str1;  
extern const string str2;  

mystring.cpp

mystring.cpp

const string str1 = "str1";  
const string str2 = "str2";

采纳答案by Marlon

Prefer the second option. If you use the first option (preprocessor), you are limiting your flexibility with the object.

更喜欢第二种选择。如果使用第一个选项(预处理器),则会限制对象的灵活性。

Consider the following... You won't be able to compare strings this way:

考虑以下...您将无法以这种方式比较字符串:

if (str1 == "some string")
{
    // ...
}

回答by Seth Johnson

If it's C++, you should use the C++ Standard Library's std::string. It's much more clear than a preprocessor macro, it will have a single location in memory when it's defined, and it has all the extra functionality of std::stringinstead of only pointer comparisons as is the case with the implicit const char*that are created with a preprocessor macro.

如果是 C++,你应该使用 C++ 标准库的std::string. 它比预处理器宏清楚得多,它在定义时在内存中只有一个位置,并且它具有所有额外的功能,std::string而不仅仅是指针比较,就像const char*使用预处理器宏创建的隐式一样。

回答by RayX

To take OO advantage of c++, I would say use struct/class.

为了利用 C++ 的 OO 优势,我会说使用 struct/class。

header:

标题:

struct Constants {
    static const string s1;
    static const string s2;
};

cpp:

cp:

const string Constants::s1 = "blah1";
const string Constants::s2 = "blah2";

To reference:

参考:

cout << Constants::s1 << endl;

回答by Matthias

If it is C++ instead of C, you should really use some variable instead of a preprocessor macro. The former is clearer than the latter. Furthermore, if you use C++17, you can use inline variables:

如果它是 C++ 而不是 C,你真的应该使用一些变量而不是预处理器宏。前者比后者更清晰。此外,如果您使用 C++17,则可以使用内联变量:

inline const std::string str = "foobar";

or

或者

// constexpr is implicitly inline
constexpr char str0[] = "foobar";
constexpr const char* str1 = "foobar";
constexpr std::string_view str2 = "foobar";

This is also clearer than using externand can be used in header-only APIs as well.

这也比 using 更清晰extern,也可以在仅标头的 API 中使用。

回答by Martin Beckett

If you don't have to use the preprocessor don't!

如果您不必使用预处理器,请不要使用!

If these strings are needed in a resource editor or a manifest or something you might have to.

如果资源编辑器或清单中需要这些字符串,或者您可能必须这样做。

回答by Daemin

You could also just use a const char* string for constant data and not a string object, since the object will need to be initialised at the start of the program with the constant data anyway. Do this if you're not going to be doing much with strings but just displaying them or printing them out as is.

您也可以只对常量数据使用 const char* 字符串而不是字符串对象,因为无论如何都需要在程序开始时使用常量数据初始化该对象。如果您不打算对字符串做太多操作,而只是按原样显示或打印它们,请执行此操作。

So:

所以:

extern const char * str1;

and

const char * str1 = "str1";

回答by R Sahu

I would suggest use of functions.

我建议使用函数。

extern const std::string& str1();
extern const std::string& str2();

This gives you more flexibility in how you get those strings in the .cpp file.

这为您在 .cpp 文件中获取这些字符串的方式提供了更大的灵活性。

回答by dma

Also consider the issue of non-POD static construction and destruction order, as described in the Google C++ style guide.

还要考虑非 POD 静态构造和销毁顺序的问题,如Google C++ 样式指南中所述

An alternative is to use:

另一种方法是使用:

const char str1[] = "str1";
const char str1[] = "str2";