我应该使用 std::string 还是 const char* 作为字符串常量?

发布于 2024-07-11 23:09:48 字数 430 浏览 5 评论 0原文

我看过这两种风格的代码,我不确定其中一种是否比另一种更好(这只是风格问题)? 您对为什么选择其中之一有什么建议吗?

// Example1
class Test {
    private:
        static const char* const str;
};

const char* const Test::str = "mystr";
// Example2
class Test {
     private:
         static const std::string str;
};

const std::string Test::str ="mystr";

I have seen code around with these two styles , I am not not sure if one is better than another (is it just a matter of style)? Do you have any recommendations of why you would choose one over another.

// Example1
class Test {
    private:
        static const char* const str;
};

const char* const Test::str = "mystr";
// Example2
class Test {
     private:
         static const std::string str;
};

const std::string Test::str ="mystr";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

我爱人 2024-07-18 23:09:48

通常,您应该更喜欢 std::string 而不是普通的 char 指针。 然而,在这里,用字符串文字初始化的 char 指针有一个显着的好处。

静态数据有两种初始化。 一种称为静态初始化,另一种称为动态初始化。 对于那些使用常量表达式初始化并且是 POD(如指针)的对象,C++ 要求它们的初始化在一开始就发生,在动态初始化发生之前。 初始化这样的 std::string 将动态完成。

如果某个类的对象是某个文件中的静态对象,并且该对象需要在初始化期间访问该字符串,则可以依赖在使用 const char* const< 时已经设置的对象/code> 版本,而使用未静态初始化的 std::string 版本时,您不知道字符串是否已初始化 - 因为跨翻译单元的对象初始化顺序边界没有定义。

Usually you should prefer std::string over plain char pointers. Here, however, the char pointer initialized with the string literal has a significant benefit.

There are two initializations for static data. The one is called static initialization, and the other is called dynamic initialization. For those objects that are initialized with constant expressions and that are PODs (like pointers), C++ requires that their initialization happens at the very start, before dynamic initialization happens. Initializing such an std::string will be done dynamically.

If you have an object of a class being a static object in some file, and that one needs to access the string during its initialization, you can rely on it being set-up already when you use the const char* const version, while using the std::string version, which isn't initialized statically, you don't know whether the string is already initialized - because the order of initialization of objects across translation unit boundaries is not defined.

老娘不死你永远是小三 2024-07-18 23:09:48

嗯, std::string 与 const char * 不同。 我通常会在使用 std::string 方面犯错误,因为它是一个具有许多附加功能的类,使其更易于使用。

如果性能至关重要,并且您使用 const char * 来提高效率,那么就这样做。

Hmmm, a std::string is not the same as a const char *. I usually err on the side of using std::string because it is a class that has many additional capabilities that make it much easier to use.

If performance is paramount and you are using const char * for efficiency, go that way.

哀由 2024-07-18 23:09:48

在使用 C++ 时,我倾向于使用 std::string 而不是 char *。 我更喜欢 std::string 主要是因为它的内置功能以及不必处理指针的便利性和安全性。

然而,正如其他人提到的,如果您过度关心性能,const char * 版本可能会更有利。 我似乎记得某个聪明的人曾经说过过早的优化是万恶之源(或类似的)。 :)

I tend to favor std::string's over char *'s when doing C++. I prefer std::string mainly for its built in capabilities as well as convenience and safety of not having to deal with pointers.

However, as others have mentioned, the const char * version may be favorable if you are overly concerned about performance. I seem to recall that someone smart once stated that premature optimization is the root of all evil (or some such). :)

小梨窩很甜 2024-07-18 23:09:48

第一个示例需要较少的管理字符串的开销(即,只需一个指向 TEXT 部分的指针)。 此外,第二种方法可能还需要堆分配以将字符串文字复制到 std:string 类缓冲区。 因此,您最终会得到数据的两个副本。

The first example requires less overhead for managing the string (i.e., just a pointer to the TEXT section). Also, the second method may require a heap allocation as well to copy the string literal to the std:string class buffer. So, you would end up with two copies of the data.

萌酱 2024-07-18 23:09:48

在涉及多个具有不同编译器和库的平台、许多团队和许多人的大型项目中,我们反复遇到静态 std::string 的问题。 在某些平台上,std:string 实现不是线程安全的。 在一个平台上,编译器优化的代码跳过了从全局静态常量初始化本地 std:string。 在解决了其中一些问题之后,我们只允许内置类型使用全局静态常量。

In large projects involving several platforms with different compilers and libraries, many teams, and lots of people we have repeatedly run into problems with static std::strings. On some platforms the std:string implementation isn't thread safe. On one platform the compiler optimized code skipped initializing a local std:string form the global static const. After chasing a few of these problems we only allow global static consts for built in types.

半寸时光 2024-07-18 23:09:48

第二个版本的优点是它具有预先计算的长度以及充实的字符串类的其他优点。 第一个的优点是唯一的初始化只是将指针分配给已加载到可执行映像中的静态数据,而第二个必须从同一指针初始化字符串。

The second version has the advantage that it comes with a pre-calculated length and the other benefits of a fleshed out string class. The first has the advantage that the only initialization is just assigning a pointer to static data already loaded in the executable image, where the second one has to initialize the string from that same pointer.

绝不服输 2024-07-18 23:09:48

首先,如果不使用 char*。 如果您想要一个 ASCIIZ 字符串,请直接定义其中之一:

const char Test::str[] = "mystr";

大多数情况下,这就是我所使用的。 为什么要为字符串类的开销浪费时间和内存呢?

请注意,“sizeof(Test::str)”将准确地给出数组的长度,即字符串的长度,包括终止 NUL (strlen(str)+1)。

First of all, if wouldn't use a char*. If you want an ASCIIZ string, define one of them directly:

const char Test::str[] = "mystr";

For the most part, that's what I'd use. Why waste time and memory for the overhead of string class.

Note that "sizeof(Test::str)" will accurately give you the length of the array, which is the length of the string, including the terminating NUL (strlen(str)+1).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文