![](/img/trans.png)
[英]How to initialize global const variables in a namespace with values from a function?
[英]Load const namespace variables values from file
我有以下代码:
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:
#include "colors.hpp"
namespace Colors
{
const sf::Color Global(50, 50, 50, 255);
const sf::Color Text(255, 255, 255, 255);
}
因此,我想知道是否有一种方法可以从计算机上的文件(例如“ colors.txt”)中加载这些变量的值。 我知道我可以使用一个类,但是我希望仅通过包含“ colors.hpp”就可以在整个程序中访问这些变量。
我会尝试这样的
#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.