I'm trying to write a code tha
I'm trying to write a code that process both scalarFields and vectorFields and I came to the point where I need something like the following. But offcourse it does not work because the scope of X is limited by the if statement scope. I tried lots of ideas including #pragama but I can not find a solution. Does any body have an idea how to accomplish this? Thanks.
if (Xheader.headerClassName() == "volScalarField") { volScalarField X(Xheader, mesh); } else if (Xheader.headerClassName() == "volVectorField") { volVectorField X(Xheader, mesh); } else { } // use X /Maka. |
Hi Maka!
You could only do
Hi Maka!
You could only do this via a pointer to the "next" common untemplated parent-class of these two classes which would be regIOobject (and I don't think that the functionality of this class is sufficient for what you propably want to do) "You need a thief to catch a thief" or like the C++-programmer says: you need a templated function to catch template instances. (Static polymorphism) How to implement this principle depends on what you want to do. Have a look at the sources of the sample-utility or decomposePar. If I remember it correctly they do similar things. Maybe somebody else can point you to a simpler example than these in the OF-sources Bernhard |
to make what I want to do more
to make what I want to do more clear instead of "// use X" the following code should be in place:
const vectorField& centres = mesh.C(); forAll(centres, celli) { // use X } That is the reason why I want to avoid constructing the volScalarField or volVectorField inside the loop since it loops over all cells in the domain while the field, to be efficient, should be constructed once outside that loop. Best regards, Maka |
I might have missed soemthing,
I might have missed soemthing, but it looks like you simply need a template function with the looping over the cell centres instead it.
|
I'm currently reading about fu
I'm currently reading about function templates and trying to see if it solves the problem. Many thanks for your help.
in more details see the following. I could solve the problem if in C++ we could store variable types. something like #include<typeinfo> type_info x = int; if (condition) x=scalar; else x=vector But it does not work. I tried using typedef but you can not redefine typedef. I can not try #def and #undef since it will have the scope problem again and it will not work since the type is determined at run time. What I want to do in full detail: if (Xheader.headerClassName() == "volScalarField") { volScalarField X(Xheader, mesh); } else if (Xheader.headerClassName() == "volVectorField") { volVectorField X(Xheader, mesh); } else { } forAll(centres, celli) { // some code if (Xheader.headerClassName() == "volScalarField") { // do something } else if (Xheader.headerClassName() == "volVectorField") { // do another thing } else { } } |
I'm currently reading about fu
Quote:
(Forget about trying to use typedefs for the same thing). |
All times are GMT -4. The time now is 16:57. |