如何存储将由许多不同的类访问的字符串常量? [英] How to store string constants that will be accessed by a number of different classes?

查看:56
本文介绍了如何存储将由许多不同的类访问的字符串常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

堆栈溢出有太多不同的答案:

There are too many different answers on stack overflow:

  1. 声明一个名称空间,并在hpp文件中将所有字符串标记为extern const,并在cpp文件中放置其定义.

  1. Declare a namespace, and in the hpp file mark all strings as extern const, and in the cpp file put their definitions.

C ++如何在cpp之间与extern共享常量-错误:指定了存储类

使用静态const代替extern const:

Use static const instead of extern const:

https://softwareengineering.stackexchange.com/questions/351827/is-it-bad-practice-to-constant-consant-using-class-static-methods

使用内联函数:

类中的静态字符串常量与名称空间中的常量[c ++]

使用匿名名称空间!

其中如何在C ++中放置常量字符串:静态类成员还是匿名名称空间?

这真是令人困惑.使用内联函数似乎是返回字符串常量的冗长而乏味的方法.

This is all really confusing. Using an inline function seems like a really lengthy and tedious way to return a string constant.

为什么我们不能只使用包含静态字符串的名称空间,这些名称空间在cpp文件中定义?

有人可以提供一个明确/明确的答案来说明如何将字符串存储在一个常量文件中,以便多个cpp文件可以访问它们吗?

在C#和Java中,将所有常量存储在常量"文件中似乎不是问题.在C ++中最简单的方法是什么?

In C# and Java it seems to be a non-issue to store all your constants in a "Constants" file. What is the easiest way to do this in C++ ?

Java中的这些答案似乎是特定且可以理解的.在C ++中,尚不清楚哪种方法可能具有最少的编译时间,最少的运行时内存使用量.

These answers in Java seem specific and understandable. In C++ it is unclear which is the way likely to have least compilation time, least memory usage at run time.

在许多类中共享Java中的常量字符串吗?

https://dzone.com/articles/constants-in-java-the-anti-pattern-1

推荐答案

这个问题范围太广,无法回答.但是,基于对注释中给出的应用程序性质(内部用于注册事件的名称)的进一步说明,我可能会建议使用类似于此类的标头

This question is way too broad to be answered. But based on further clarifications concerning the nature of the application (names used internally for registering events) given in comments, I would probably suggest something along the lines of using a header like this

#ifndef INCLUDED_EVENT_NAMES
#define INCLUDED_EVENT_NAMES

#pragma once

#include <string_view>

namespace event_names
{
    using namespace std::literals;

    inline constexpr auto name1 = "value1"sv;
    inline constexpr auto name2 = "value2"sv;
}

#endif

使用 std :: string_view 常量而不是普通的 const char * const char [N] 常量意味着您知道每个常量的长度字符串,不必依赖空终止.使用 std :: string 几乎肯定会在运行时带来内存开销和初始化成本.此处定义的 std :: string_view 将编译为直接引用静态分配的字符串文字对象的代码.即使在多个转换单元(.cpp文件)中使用相同的常量时,现代编译器也几乎可以肯定地将相同的字符串文字在二进制文件中存储一次(由

Using std::string_view constants rather than plain const char* or const char[N] means that you know the length of each string and don't have to rely on null termination. Using std::string would almost certainly entail memory overhead and initialization cost at runtime. The std::string_views defined here will compile down to code that's directly referring to statically-allocated string literal objects. Even when the same constant is used in multiple translation units (.cpp files) modern compilers will almost certainly store the same string literal in the binary only once (a standard optimization enabled by [lex.string]/15)

如果您坚持使用C ++ 11,那么为所需的字符串文字对象做一些命名引用是最简单的(并且很可能足以完成您的工作)

If you're stuck with C++11, then it's simplest (and most likely sufficient for what you need to do) to just make some named references for the string literal objects you need:

#ifndef INCLUDED_EVENT_NAMES
#define INCLUDED_EVENT_NAMES

#pragma once

namespace event_names
{
    constexpr auto& name1 = "value1";
    constexpr auto& name2 = "value2";
}

#endif

由于引用不是对象,因此任何人都不可能做任何事情(偶然或其他方式),而这会导致为常量本身创建实际对象(当然,字符串文字对象除外).另外,由于它是一个引用,因此它将携带有关数组大小的信息,以防万一任何东西都可以利用该信息(请注意,该大小包括终止null).而且由于仍然存在对 const char * 的隐式转换,因此您可以在需要简单的旧C字符串的任何地方使用它们.

Since references are not objects, it is impossible for anyone to do anything (accidentally or otherwise) that would cause an actual object to be created for the constant itself (except for the string literal object of course). Also, since it's a reference, it will carry the information about the size of the array, in case anything may be able to take advantage of that (be aware that that size includes the terminating null). And since there still is the implicit conversion to const char* you will be able to use these whereever a plain old C-string is required.

这篇关于如何存储将由许多不同的类访问的字符串常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆