简体   繁体   中英

Load const namespace variables values from file

I have the following code:

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);
}

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". I know I could use a class, but I want these variables to be accessible in the whole program by just including "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")};
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM