简体   繁体   English

在C ++中定义类字符串常量?

[英]Defining class string constants in C++?

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";

Usually you should prefer std::string over plain char pointers. 通常你应该更喜欢std::string不是普通的char指针。 Here, however, the char pointer initialized with the string literal has a significant benefit. 但是,这里使用字符串文字初始化的char指针具有显着的好处。

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. 对于那些使用常量表达式初始化并且是POD(如指针)的对象,C ++要求在动态初始化发生之前,在初始阶段进行初始化。 Initializing such an std::string will be done dynamically. 初始化这样的std :: string将动态完成。

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

Hmmm, a std::string is not the same as a const char *. 嗯,std :: string与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. 我通常在使用std :: string时犯错,因为它是一个具有许多附加功能的类,使其更易于使用。

If performance is paramount and you are using const char * for efficiency, go that way. 如果性能是最重要的,并且您使用const char *来提高效率,那就这样吧。

I tend to favor std::string's over char *'s when doing C++. 在做C ++时,我更倾向于使用std :: string而不是char *。 I prefer std::string mainly for its built in capabilities as well as convenience and safety of not having to deal with pointers. 我更喜欢std :: string,主要是因为它内置的功能以及不必处理指针的方便性和安全性。

However, as others have mentioned, the const char * version may be favorable if you are overly concerned about performance. 但是,正如其他人所提到的,如果您过分关注性能,那么const char *版本可能会有利。 I seem to recall that someone smart once stated that premature optimization is the root of all evil (or some such). 我似乎记得有人聪明曾经说过早优化是所有邪恶(或某些此类)的根源。 :) :)

The first example requires less overhead for managing the string (ie, just a pointer to the TEXT section). 第一个示例需要较少的管理字符串的开销(即,只是指向TEXT部分的指针)。 Also, the second method may require a heap allocation as well to copy the string literal to the std:string class buffer. 此外,第二种方法也可能需要堆分配以将字符串文字复制到std:string类缓冲区。 So, you would end up with two copies of the data. 因此,您最终会获得两份数据副本。

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. 在涉及具有不同编译器和库的多个平台的大型项目中,许多团队和许多人一直遇到静态std :: strings的问题。 On some platforms the std:string implementation isn't thread safe. 在某些平台上,std:string实现不是线程安全的。 On one platform the compiler optimized code skipped initializing a local std:string form the global static const. 在一个平台上,编译器优化代码跳过从全局静态const初始化本地std:string。 After chasing a few of these problems we only allow global static consts for built in types. 在追逐其中一些问题之后,我们只允许内置类型的全局静态consts。

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. 第一个优点是唯一的初始化只是为已经加载到可执行映像中的静态数据指定一个指针,其中第二个初始化必须从同一个指针初始化字符串。

First of all, if wouldn't use a char*. 首先,如果不使用char *。 If you want an ASCIIZ string, define one of them directly: 如果需要ASCIIZ字符串,请直接定义其中一个:

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

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

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