简体   繁体   English

从文件加载const名称空间变量值

[英]Load const namespace variables values from file

I have the following code: 我有以下代码:

colors.hpp: colors.hpp:

#ifndef COLORS_HPP
#define COLORS_HPP

#include <SFML/Graphics/Color.hpp>

namespace Colors
{
    extern const sf::Color Global;
    extern const sf::Colot Text;
}

#endif // COLORS_HPP

colors.cpp: colors.cpp:

#include "colors.hpp"

namespace Colors
{
    const sf::Color Global(50, 50, 50, 255);
    const sf::Color Text(255, 255, 255, 255);
}

And so I wanted to know if there was a way to load the values for those variables from a file on the computer, like for example "colors.txt". 因此,我想知道是否有一种方法可以从计算机上的文件(例如“ colors.txt”)中加载这些变量的值。 I know I could use a class, but I want these variables to be accessible in the whole program by just including "colors.hpp". 我知道我可以使用一个类,但是我希望仅通过包含“ colors.hpp”就可以在整个程序中访问这些变量。

I would try something like this 我会尝试这样的

#include "colors.hpp"

sf::Color loadFromFile(std::string const& filename) {
   sf::Color result;
   // open file 'filename'
   // update result
   return result;
}

namespace Colors
{
    const sf::Color Global{loadFromFile("file1")};
    const sf::Color Text{loadFromFile("file2")};
}

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

相关问题 如何使用函数中的值初始化名称空间中的全局const变量? - How to initialize global const variables in a namespace with values from a function? 使用文件输入中的值初始化类中的const成员变量 - Initialise const member variables in class using values from file input 如何从C ++中的配置文件加载自定义“ const”变量? - How do I load custom “const” variables from a config file in C++? 如何从文件加载const变量的内容? - How to load content of a const variable from a file? 从文本文件读取const变量以获取多维数组 - Reading const variables from a text file for multidimensional array 我应该在类或名称空间范围使用静态const变量吗? - Should I prefer static const variables at class or namespace scope? 从文本文件加载变量的最简单方法 - Easiest way to load variables from a text file 将错误与指向替代名称空间中的枚举的静态const值链接 - Linking errors with static const values referring to enum in alternative namespace 头文件中的const变量和静态初始化fiasco - const variables in header file and static initialization fiasco 实现文件中命名的命名空间,用于对const字符串文字进行分组 - 好/坏? - Named namespace in implementation file to group const string literals - Good/Bad?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM