In my program I have defined a header constants.h
where I define constants that will be used in my program, which contains multiple classes. No I would like to read the constants from the command line and initialize these constants such that they can be used in the same way as before.
constants.h:
const int FOO = 10;
classA.cpp:
#include "constants.h"
// uses FOO
classB.cpp:
#include "constants.h"
// uses FOO
My idea was to read the value for FOO
and pass it over to the two classes as member variables when I creat the object in the main class. But I dont think this is a good idea, because of redundancy.
Your program has multiple classes, so I suppose you are programming using Object-oriented paradigm - global variables are discouraged in such projects. Of course introducing information redundancy is also bad.
What you can do is make a new class that would store all constants and would be responsible for reading them in command line. Then you can store references to objects of this class in
classA
andclassB
and wherever else you need those constants