I'm writing my program in C++11. I have a main class called MyExperiment. In this class there is another class, ReadFromAFile, which reads out some information about the set-up from a file. Then I want to use the data to initialise some other object which are members of the MyExperiment class. This is how it should look like:
class MyExperiment
{
FirstObject *firstObject;
SecondObject *secondObject;
public:
MyExperiment()
{
ReadFromAFile readfromafile;
readfromafile.read();
firstObject = new FirstObject(/*some data from the readfromafile object */);
secondObject = new SecondObject(/*some data from the readfromafile object */);
}
}
Each object (firstObject and secondObject) class consists of some information that can (and should) be read out from a file and of some other variables and functions. I really don't want to have two classes for each object type: one for the data that can be read from the file and the other for the rest.
So, I want to read some infromation from a file using the ReadFromAFile class and then somehow pass the information to the construtors of my objects. My question is: what is the right way to do it?
Right now I have some structures which describe the data that I read from a file in the ReadFromAFile class. Each structure refers to one object. I have also getters functions which are invoked in the constructor of the MyExperiment class:
class MyExperiment
{
FirstObject *firstObject;
SecondObject *secondObject;
public:
MyExperiment()
{
ReadFromAFile readfromafile;
readfromafile.read();
firstObject = new FirstObject(readfromafile.getFirstStruct());
secondObject = new SecondObject(readfromafile.getSecondStruct());
}
}
I don't think this solution is elegant and smart. I have a structure which depicts my firstObject and then I use it the constructor of this object.
I have no idea how to do it in a better way, so I'll be grateful for any remarks.
I don't want to read the file in my myExperiment class, because it's already complicated, so I need some enacpsulation.
Is there a workaround for this problem?
Aucun commentaire:
Enregistrer un commentaire