I have a class that has several member classes as attributes. The constructor of the class will take a filename of a byte file. The different member classes use subsequent parts of the file in their constructors, lets call them part A, B and C. The size of the file will vary.
Using the heap I would do something like this:
class myClass
{
myClass(char *filename)
{
std::ifstream inputFile(filename, std::ios::binary);
m_Class1 = new ClassA(inputFile); // read part A
m_Class2 = new ClassB(inputFile); // read part B
m_Class3 = new ClassC(inputFile); // read part C
inputFile.close();
}
}
I would like to do this on the stack instead of the heap.
Initialization lists come to mind, but for that I would have to waste time re-reading the redundant part of the inputfile to get to the part needed for each member class.
I don’t know if this is just a terrible approach (most likely), but does anyone have any suggestions towards accomplishing this effectively? Or suggestions for another way of organizing this?
Note that the declaration order of the members is important.