Ok, I'm writing a medium size OOP app (C++ to be specific) and I want to learn as muchapplication in C++ as I can about good OOP practise, so I wanta way to do it the "OOP style"practice OOP principles.
I have several classes in my project (surprise!), and some of them need to access run-time configuration parameters. These parameters are read from several sources during application start-up, some. Some are read from a config file in the users home-dir, some are command line arguments (argv).
Thus,So I ended up to implementcreated a class "ConfigBlock"ConfigBlock. This class reads all the parameter sources and stores it in aan appropriate data structure. Examples are path- and filenames which can be changed by user in the config file, or the --verbose CLI flag. Then, one can call GetVerboseLevel() andConfigBlock.GetVerboseLevel() in order to read this specific parameter.
FirstMy question: isIs it good practisepractice to collect all such runtime config data in one class?
Then, my classes need access to all these parameters. I can think of several ways to achieve this, but I'm not sure which one to take. A class' constructor can be a given a reference to my ConfigBlock, like
public:
MyGreatClass(ConfigBlock &config);
Or they just include a header "CodingBlock.h" which contains a definition of my CodingBlock:
extern CodingBlock MyCodingBlock;
Then, only the classes .cpp file needs to include and use the ConfigBlock stuff.
The .h file does not introduce this interface to the user of the class.
However, the interface to ConfigBlock is still there, however, it's hidden from the .h file.
Is
Is it good to hide it this way?
I want the interface to be as small as possible, but in the end, I guess every class which needs config parameters has to have a connection to my ConfigBlock. But, what isshould this connection lookinglook like?