CFD Online Logo CFD Online URL
Home > Forums > OpenFOAM

C question how to initialize a variable amount of objects

Register Blogs Members List Search Today's Posts Mark Forums Read

LinkBack Thread Tools Display Modes
Old   February 21, 2009, 06:58
Default Hello All, Not an exact Foa
Senior Member
Mark Couwenberg
Join Date: Mar 2009
Location: Netherlands
Posts: 130
Rep Power: 8
markc is on a distinguished road
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,

markc is offline   Reply With Quote

Old   February 21, 2009, 07:06
Default With this initialiser you will
Senior Member
Hrvoje Jasak
Join Date: Mar 2009
Location: London, England
Posts: 1,768
Rep Power: 21
hjasak will become famous soon enough
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.

Hrvoje Jasak
Providing commercial FOAM/OpenFOAM and CFD Consulting:
hjasak is offline   Reply With Quote


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
a question about initialize in fluent zhaopu FLUENT 0 December 29, 2008 06:52
Setting initial amount of ideal gas David FLUENT 0 November 2, 2007 09:35
Amount of Gas Suction Im FLUENT 0 January 20, 2005 22:29
Tecplot 10:Question about internal |LOOP| variable Matt Morse Tecplot 1 July 21, 2004 10:26
mesh nodes amount?? newcfd CFX 2 April 27, 2004 00:17

All times are GMT -4. The time now is 03:54.