CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Pre-Processing (https://www.cfd-online.com/Forums/openfoam-pre-processing/)
-   -   C scope resolution problem (https://www.cfd-online.com/Forums/openfoam-pre-processing/62086-c-scope-resolution-problem.html)

maka March 3, 2008 12:46

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.

gschaider March 3, 2008 14:40

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

maka March 4, 2008 08:33

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

olesen March 4, 2008 09:07

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.

maka March 4, 2008 10:00

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 {

}

}

olesen March 5, 2008 05:38

I'm currently reading about fu
 
Quote:

I'm currently reading about function templates and trying to see if it solves the problem.
Keep reading - it should solve your problem.
(Forget about trying to use typedefs for the same thing).


All times are GMT -4. The time now is 17:18.