CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM (https://www.cfd-online.com/Forums/openfoam/)
-   -   A general C question (https://www.cfd-online.com/Forums/openfoam/60765-general-c-question.html)

niklas September 8, 2008 09:42

On my spare time I'm working o
 
On my spare time I'm working on a personal project and since my C++ knowledge is pretty bad I'm hoping that someone here might be able to help me.

I'd like to construct all my variables via a file.
All that is required as input is the filename.
The constructor (in the file) would look something like this.

typeName keyWord " string of data depending on typeName "

so to construct a scalar would look like this:
scalar a 1.0;

to construct a derived class like scalarList, or vector<scalar>, would be:
scalarList d ( 1.0 2.0 3.0 );

the variables will be constructed via a stream so Im thinking that the constructor ideally would use a call that would look something like this

ifstream file;
string name;
string typeName;

file >> typeName >> name;
variable<typename> a = variable<typename>(name,file);

so depending on the value of typeName Id like the constructor to read the appropriate thing.
The problem here is that typeName is not a class, its just a string.

I dont want to do it by adding an if-statement for every implemented type.
Is this possible using templated or virtual functions or am I attacking the problem completely wrong?

N

grtabor September 8, 2008 11:38

OpenFOAM of course parses its
 
OpenFOAM of course parses its files; i.e. it goes through the file character-by-character constructing keywords until it recognises something. I'm not sure that that is what you are trying to do here.

What I think you want to do is to derive all "things" from some common base class Thing which has a virtual function Read (which will probably be overloaded in derived classes). When you are looping around reading in the list of things, everything will be a Thing and so will have the .Read() function available.

Gavin


All times are GMT -4. The time now is 15:10.