I have a complex Parameters
object, which I'd like to use to parse CLI arguments and which contains all relevant parameters for the rest of the program. I also would like the class to do some preprocessing, so it is directly constructing objects from the parameters for later use. Something like:
class Parameters {
Parameters(int argc, char* argv[]);
ComplexObject complexParameter;
int parameter1;
std::string parameter2;
/* ... multiple parameters ... */
}
Parameters::Parameters(int argc, char* argv[]) {
/* parsing in simple parameters using getopts and a long switch case structure*/
complexParameter = ComplexObject(parameter1, parameter2 /*...*/);
}
The problem is, ComplexObject
doesn't have a default constructor, but the constructor of Parameters
requires me to initialize complexParameter
(for example by using a initializer list). Initializing complexParameter
with some stand-in values and later replacing it doesn't seem like a good solution to me, and I don't know how I would use a initializer list in this case.
What would be the best way to circumvent this? Should I use a factory method Parameters::create()
where I construct a Parameters
object using a really long constructor like
Parameters(parameter1, parameter2, /*...*/ ComplexObject(parameter1, parameter2))
at the end? Or should I put the construction of more complex objects in the main, or further compartimentalize my Parameters
structure? Or should I use a (smart) pointer for ComplexObject
? In the last case, I would need to create one with new
and also take care of destruction of it, right? I just can't think of an easy solution. I guess I'm just not used to the fact that in C++ objects are not nullable. In other languages I'd just initiliaze it at the end of the constructor.
Aucun commentaire:
Enregistrer un commentaire