There are a lot of ways of doing this, but as a scientific programmer I am making an effort to redo this project in a more elegant way, to try to encounter more scenarios that I might face in a job interview.
I have an FDTD Maxwell equation solver. There is a database of materials that I want to read in, and certain materials will later be mapped to the cells in the mesh.
There are several classes of materials (linear non-dispersive, lorentz, drude, hydrodynamic) and they each need a couple of specific variables and will entail a unique update routine. It seems natural to have all of the common details and some virtual functions stored as a base class, and have the extra variables that each material type needs implemented as a more specific inherited class.
Now how do I read in and access an arbitrary number of these things? From what I understand, if I have a vector of pointers of the base type, I will not be able to use it to access the members specific to the derived type. I need to know the specifc type in advance to do a dynamic cast, and there will be lots of these things parsed from a user edited text file.
I think the solution might be to keep a concurrent vector of short ints (or a variable in the base class) along with the vector of base pointers, where the ints specify the inherited type of that object and then I can dynamic_cast from a case selector. It does need to be fast. This access occurs in the innermost loop of a time-domain field solver.
Currently, each material just has a lot of unused members and an integer to specify which subclass of materials it is. Every routine that uses the material checks the integer and only accesses the appropriate members. I want to do this in a safer, more industry standard OOD approach, largely as an excercise.