简体   繁体   English

C ++中类似Python的静态数据存储?

[英]Python-like storage of static data in C++?

When I have test data objects, or other static data objects such as SQL strings, I like to keep them out of application code. 当我拥有测试数据对象或其他静态数据对象(例如SQL字符串)时,我喜欢将它们置于应用程序代码之外。 In Python, this is done with a separate source file and an import as shown below. 在Python中,这是通过一个单独的源文件和一个导入来完成的,如下所示。 How is static data organized and used C++? 静态数据如何组织和使用C ++?

 (file: testdata.py)
x = Foo() (an object)
x.name = "Bar"
x.number = 123
..

(file: test.py)
import testdata.py
testObject1 = testdata.x 
..

The most flexible choice for connection strings etc. would be to create a settings file (with a format of your own choosing) and a class that can read the file, since this allows you to change the settings without recompiling the program. 连接字符串等最灵活的选择是创建一个设置文件(使用您自己选择的格式)和一个可以读取该文件的类,因为这使您无需重新编译程序即可更改设置。

For static data that you want to include in the code, you can use static class members: 对于要包含在代码中的静态数据,可以使用静态类成员:

// file.h
class Foo {
public:
    static string getBar();
private:
    static string bar;
};

// file.cpp
Foo::bar = "qwe";

static Foo::getBar() {
    return bar;
}

There are lots of possibilities. 有很多可能性。 This also depends on what you're trying to do (eg do you want the values to be editable after compiling or only before compiling? 这也取决于您要执行的操作(例如,您希望这些值在编译之后还是仅在编译之前是可编辑的?

As for the later, you could use a separate translation unit. 至于以后,您可以使用一个单独的翻译单元。

In some header file: 在一些头文件中:

extern const char *name;
extern const int number;

In one separate source file: 在一个单独的源文件中:

const char *name = "Bar";
const int number = 123;

However, you could do the class/struct approach, too: 但是,您也可以使用类/结构方法:

Header file: 头文件:

struct Foo {
    const char *name;
    int number;
};

extern const Foo Bar;

Source file: 源文件:

const Foo Bar = {"Bar", 123};

You can put them in another file as well; 您也可以将它们放在另一个文件中。 extern data declarations in an header file, and the definitions in a .cpp file, like this: 头文件中的extern数据声明以及.cpp文件中的定义,如下所示:

// Foo.h

extern Foo afoo;
extern int anint;

// Foo.cpp

Foo afoo;
int anint = 0;

Then you can #include "Foo.h" wherever you want to use afoo and anint . 然后,您可以在要使用afooanint任何地方#include "Foo.h"

There are many ways to store data out of the source code. 有很多方法可以将数据存储在源代码之外。

For example, configuration files are stored in XML for example, though (fortunately) JSON has been gaining some grounds (not as much as I'd like to), and there are also custom formats. 例如,配置文件存储在XML中,尽管(幸运的是)JSON已经获得了一些基础(不如我想要的那样多),并且还有自定义格式。

Another example, for translation you might use gettext and store strings in .po files, and add new languages without recompiling. 另一个示例,对于翻译,您可以使用gettext并将字符串存储在.po文件中,并添加新语言而无需重新编译。

It all works the same way, in the end: 最后,它们的工作方式都相同:

  • you define a format for the file 您定义文件的格式
  • you create a reader (and perhaps writer) procedure that converts between the file format and the in-memory format 您创建一个读取器(也许是写入器)过程,该过程在文件格式和内存格式之间进行转换
  • you find a way to pass the file path to the program: fixed name in current directory, fixed name in home directory, command line argument, environment variable, etc... 您找到一种将文件路径传递给程序的方法:当前目录中的固定名称,主目录中的固定名称,命令行参数,环境变量等。

If you wish to do it as you did in Python, you could also compile the resources into their own DLL and pull them at runtime while loading the symbols, changing the resources would only require re-compiling this one DLL. 如果您希望像在Python中那样进行操作,则还可以将资源编译到它们自己的DLL中,并在运行时加载符号时将它们拉出,更改资源只需要重新编译一个DLL。

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

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