![](/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.