|
[Sponsors] |
March 3, 2008, 13:46 |
I'm trying to write a code tha
|
#1 |
Senior Member
Maka Mohu
Join Date: Mar 2009
Posts: 305
Rep Power: 18 |
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. |
|
March 3, 2008, 15:40 |
Hi Maka!
You could only do
|
#2 |
Assistant Moderator
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51 |
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
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request |
|
March 4, 2008, 09:33 |
to make what I want to do more
|
#3 |
Senior Member
Maka Mohu
Join Date: Mar 2009
Posts: 305
Rep Power: 18 |
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 |
|
March 4, 2008, 10:07 |
I might have missed soemthing,
|
#4 |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,714
Rep Power: 40 |
I might have missed soemthing, but it looks like you simply need a template function with the looping over the cell centres instead it.
|
|
March 4, 2008, 11:00 |
I'm currently reading about fu
|
#5 |
Senior Member
Maka Mohu
Join Date: Mar 2009
Posts: 305
Rep Power: 18 |
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 { } } |
|
March 5, 2008, 06:38 |
I'm currently reading about fu
|
#6 | |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,714
Rep Power: 40 |
Quote:
(Forget about trying to use typedefs for the same thing). |
||
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Scope of CFD in aerospace engineering | Apoorva L | Main CFD Forum | 2 | April 6, 2011 15:25 |
OF14 compilation error bistreamb is already declared in this scope | rafal | OpenFOAM Installation | 5 | August 26, 2008 12:23 |
scope of turbulent transition | Prapanj | Main CFD Forum | 0 | August 20, 2007 14:39 |
cfd-scope,future and changes | shiva | Main CFD Forum | 11 | August 6, 2005 11:30 |
cfd market scope | narasimma varman | Main CFD Forum | 0 | November 5, 2002 04:37 |