PDA

View Full Version : for i = 0 to nr members in a class ...



qt_gotcha
6th March 2010, 08:02
I want to intialize a certain type of element (a class called *TMMap) that I have in my main class TWorld. There are a lot of these elements and they all have different names. I need all of them to be created and initisalized and to a certain value. This is done in a function NewMap(0).
Can I find all members of a class of a certain type?
something like:

for (i = 0; i < nr of members in class TWorld; i++)
if (member is of type TMMap)
TMMap *T = (typecasting TMMap) member(i);
T = NewMap(0);

wysota
6th March 2010, 09:14
Do TWorld and TMMap inherit QObject?

aamer4yu
6th March 2010, 13:22
I need all of them to be created and initisalized and to a certain value.
That can be done in the constructor of the class,,, isnt it ?

If you want new objects to be initialized to a certain value, then -

QList<TMMap *> tmmapList;
for(index =0; index < numberOfObjectsToBeCreated; index++)
{
TMMap * map = new TMMap(); // Do the intialization in the ctor.
tmmapList.append(map);
}

If you want already created objects to be set to some defalt value then -

QList<TMMap *> tmmapList; // already populated.
for(index =0; index < tmmapList.count() ; index++)
{
TMMap * map = tmmapList.at(index);
map->setToDefaultValues(); // Provide this function in the class
}

qt_gotcha
6th March 2010, 17:18
TWorld depends on object (it is a thread actually), tmmap not (but it could be).

sorry for not being so clear
thanks, well they can't be initialized in the contrustor because they have a **double structure with rows and cols that depends on the user database. so that is done at run time. sorry for not telling that.
I understand you can make a list of pointers, but then each map is refernced by a number instead of its name, which has a meaning to understand the code. If all variables were referenced as a list number I would get completely lost in the code (its a waterbalance/soil erosion model using grid maps and the maps have names like discharge, vvelocity etc.). If I use numbers the code would become unreadable. These are my variables now:

TMMap *tm, *Mask, *DEM, *DX, *Grad, *LDD, *Outlet, *RainZone, *N, *RR, *MDS,
*Rain, *RainCum, *RainNet, *RainIntensity, *RainM3, *CStor, *Interc,
*WH, *WHinf, *WHroad, *WHrunoff, *WHstore, *WaterVol, *WaterVolRunoff, *InfilVolKinWave, *InfilVol, *fpa,
*FlowWidth, *V, *Alpha, *Q, *Qoutflow, *Qn, *Qs, *Qsn, *q, *R, *Perim, *WheelWidthDX, *StoneWidthDX,
*SoilWidthDX, *GullyWidthDX, *RoadWidthDX, *WheelWidth, *StoneFraction, *CompactFraction, *CrustFraction,
etc etc

can I use Qmetaobject somehow? thanks

squidge
6th March 2010, 19:38
You could use numbers and then use enumerations to give those numbers names. Then you refer by name and the compiler converts to number at runtime.