[英]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.