CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM (http://www.cfd-online.com/Forums/openfoam/)
-   -   C question how to initialize a variable amount of objects (http://www.cfd-online.com/Forums/openfoam/60700-c-question-how-initialize-variable-amount-objects.html)

markc February 21, 2009 06:58

Hello All, Not an exact Foa
 
Hello All,

Not an exact Foam question. However, maybe someone can give some advice.
I made a class which does various tasks on a certain patch in a mesh. It reads in data from a dictionary, calculate things etc etc. This works fine when needed on 1 patch in the mesh. However, now I want it to perform on more patches, e.g. 2 or 3, depending on what is given in the dictionary. I have to pass one variable (a pointer to the mesh) to each object.
In C++ you can dynamically assign objects in an array using the keyword "new". However it seems not possible to also pass variables in that array.
An example:

Having a class called CRectangle, with the following assignement you create 2 objects of that class:
>>>
CRectangle * bobby = new CRectangle[2];
<<<

But if you also want to pass arguments, like e.g.:
>>>
CRectangle * bobby = new CRectangle[2](2,3);
<<<
is not possible. You get the following error message:
>>>
error: ISO C++ forbids initialization in array new
<<<
This really seems strange to me. In my case I found some very ugly work around (hard coded initialisation of e.g. 10 objects (object1, object2, etc) and call their members only for the objects I really need.
There must be a better way. Can someone show me the way?

Thanks in advance,

Mark

hjasak February 21, 2009 07:06

With this initialiser you will
 
With this initialiser you will always call the null constructor for the CRectangle, which is not what you want. I would recommend you make a PtrList of CRectangle and then add them to the list one at a time, using whatever constructor you prefer.

Hrv


All times are GMT -4. The time now is 02:50.