从文件中加载 const 命名空间变量值
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"。我知道我可以使用 class,但我希望只需包含 "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")};
}
我有以下代码:
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"。我知道我可以使用 class,但我希望只需包含 "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")};
}